0

Problem: I am trying to test some hardware using the py.test framework (their fixtures are really useful, there's no other framework with such functionality as far as I am aware). The thing is, the running thread seems to block out any other functionality in my test.

I've tried launching the hardware_fixture running logic in a thread - yet, this doesn't seem to work for me - the thread stops responding once the time.sleep call is done.

Example:

import pytest

from time import sleep

from hardware_library import hardware_fixture


def hardware_test(hardware_fixture):
    hardware_fixture.start_the_testing_sequence()
    sleep(120)  # or whatever

Here's the thing - my hardware_fixture seems blocked by the time.sleep call.

Shall I fork the py.test, and modify it to run the test execution in a thread, or is there a simpler, better way?

How can I make everything work? I would really enjoy the non-blocking sleep.

kehrazy
  • 1
  • 1

2 Answers2

0

time.sleep() will always block the current thread. To let other threads run you should do time.sleep(0). Have fun!

PS: There is an old example that may be useful for your purposes, check this out: https://stackoverflow.com/a/93179/2864517 Just take in count that xrange from that example does not exist anymore. If you want to test the script, replace it with list(range(x,y)). Enjoy :)

(edit added the ps)

Don Pato
  • 41
  • 3
0

If you want to avoid blocking the execution of other functionality while using time.sleep() in your test, you can consider using asynchronous programming. Instead of using threads, you can utilize asynchronous frameworks like asyncio to achieve non-blocking behavior.

Here's an example of how you can modify your code using asyncio:

import pytest
import asyncio

from hardware_library import hardware_fixture


async def hardware_test(hardware_fixture):
    hardware_fixture.start_the_testing_sequence()
    await asyncio.sleep(120)  # or whatever


@pytest.mark.asyncio
async def test_hardware():
    hardware = hardware_fixture()
    await hardware_test(hardware)