1

I am trying to create a test for set some values in a device and then query them from device to assert that they are equal several times with specific time intervals. But for this to work I need to run query and assert things async because I cannot wait for a response to continue. But I couldn't use then functions with pytest.mark.asyncio. How can I use both of them at the same time?

I first tried this but I get "coroutine 'test_get_value' was never awaited" error.

@pytest.mark.asyncio
@then(parsers.parse("The query result for {setting} is {value:n}"))
@then(parsers.parse("The query result for {setting} is {value:bool}", extra_types={"bool": bool}))
async def test_get_value(destination, setting, value):
    responses = await run_queries(destination, setting)
    for response in responses:
        assert response == value

run_queries funciton is something like this. There are some custom classes that I use that is unrelated to my problem because they work without any problem.

async def run_queries(destination: int, query_name: str, connection_file: str) -> List[int]:
    # I create a custom message object with query_name, connection file, and destination

    responses = client.execute(message) # Here the query is executed and responses are collexted works without a problem
    query_result = []

    for response in responses:
        if response.tid == tid: # To check that I get the correct responses
            query_result.append(response.value[0])

    return query_result

Then without changing run_queries I tried to use a scenario mark but in that case scenario is not running at all

@scenario('../features/scenario_fixture.feature', 'Hope this works')
@pytest.mark.asyncio
def test_query():
    pass

and Feature file includes only this

Feature: Scenario test
  Scenario: Hope this works
    Then The query result for setting is 100
  • I am trying some random tests and figured out that `@scenario('async_scenario.feature', 'Hope this works')\n@pytest.mark.asyncio\n async def test_mytest():\n await random_wait()` do not recognize `@pytest.mark.asyncio` but when I delete `@scenario('async_scenario.feature', 'Hope this works')` it works as it should work. – FroMoSouOmou Jun 29 '23 at 10:05
  • would you post that as an answer to this SO question? It's valid to answer your own question, and it's a bit difficult to read the code as a comment. – Jon Wolski Jul 07 '23 at 23:29
  • 1
    @JonWolski it is not exactly a solution, I couldn't make pytest-bdd and pytest-asyncio decorators work at the same time. I really tried to using async functions with bdd but it is not really supported I guess. – FroMoSouOmou Jul 10 '23 at 07:51

0 Answers0