pytest-asyncio: unittest support: Coroutine was never awaited
I am trying to create a test case for the below async function get_data. I get Runtime warning as RuntimeWarning: coroutine 'TestAsyncStatSvc.test_async_get_data' was never awaited testfunction(**testargs)
Below is my code. Please guide on this one. AFAIK, we get this exception when there is no event loop so I created one in the fixture. What is there that I am missing?
async def get_data(self, data_id=None):
sql = """
SELECT id, description
FROM data_table
"""
sql_params = []
if data_id:
sql += """
WHERE id = $1
"""
sql_params += [data_id]
result = await self.dbproxy_async.execute_query_async(sql, sql_params)
if result.empty and data_id:
raise NotFound('data %s not found.' % data_id)
return result.to_json()
Below Is the test case:
class TestAsyncStatSvc(object):
TEST_DATA = {
'Key1': ['Key1', 'Data desc for key1'],
'Key2': ['Key2', 'Data desc for key2'],
None: [
['Key1', 'Data desc for key1'],
['Key2', 'Data desc for key2']
]
}
@pytest.mark.asyncio
async def test_async_get_data(self, data_svc_fixture):
for query_value in TESTDATA.keys:
execute_stub = MagicMock(return_value=self.TESTDATA[query_value])
# Wrap the stub in a coroutine (so it can be awaited)
execute_coro = asyncio.coroutine(execute_stub)
# Stub the database db_proxy
db_proxy = MagicMock()
db_proxy.execute_query_async = execute_coro
result = data_svc_fixture.get_data(data_id=query_value)
assert result == self.TESTDATA[query_value]
if query_value is not None:
assert len(comm) == 1
else:
assert len(comm) == 2
with pytest.raises(NotFound):
data_svc_fixture.get_data('Unobtained')
And here are the fixtures:
class Context:
def __init__(self, loop, svc_fixture):
self.loop = loop
self.svc_fixture = svc_fixture
@pytest.yield_fixture(scope="function")
def data_svc_fixture(db_proxy_fixture, get_service_fixture, my_svc_configurator_fixture, event_loop):
ctx = Context(event_loop, get_service_fixture('MySvc'))
yield ctx
event_loop.run_until_complete(asyncio.gather(*asyncio.Task.all_tasks(event_loop), return_exceptions=True))
@pytest.yield_fixture()
def event_loop():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
yield loop
loop.close()
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 19 (6 by maintainers)
Commits related to this issue
- Fix #77, broken select() result indexind — committed to smagafurov/pytest-asyncio by rudyryk 7 years ago
- 🔧 MAINTAIN: Reduce test warnings (#4742) This commit reduces the number of pytest warnings of the test suite, from 719 to 122: - Replace `collections` with `collections.abc` - pytest-asyncio do... — committed to aiidateam/aiida-core by chrisjsewell 3 years ago
- 🔀 MERGE: `develop` -> `master` * Dependencies: bump cryptography to 3.2 in `requirements` (#4520) Bumps `cryptography` from 2.8 to 3.2. Signed-off-by: dependabot[bot] <support@github.com> Co-... — committed to aiidateam/aiida-core by chrisjsewell 3 years ago
@mlaradji Got recently the same trouble and solved it like you with aiounittest. But when using aiounittest you do not need the decorator @pytest.mark.asyncio and the module pytest-asyncio anymore. I think pytest-asyncio does not work with classes derived from TestCase at all, just like the previous discussion indicate. Looking forward to when you guys solve this issue 😃
Actually marks are applied to
unittest.TestCasesubclasses because their methods are translated into pytest items. This is easy to demonstrate:Now if
@pytest.mark.asynciocan work withunittest.TestCasesubclasses is another matter because pytest uses aunittestrunner to run the test function; not sure how that plays withpytest-asyncio.Correct me if I’m wrong, but pytest markers make no sense if applied to
unittest.TestCasederived tests.pytest-asynciojust doesn’t apply own custom test runner totest_barmethod because the method is written in unittest style.@Akshita6 Just in case you didn’t check which pulgins are loaded when you run
pytest… I had the same error. In my casepytest-asynciowasn’t installed properly.Hey @seifertm! Thank you for the reply. Yes, they are!
The first time I read the repo I didn’t notice that, when I hit this wall I re-read and noticed that. I was able to work it out using only
unittest.IsolatedAsyncioTestCase.I seem to have resolved the issue by using
aiounittest(only 1 contributer unfortunately) instead ofunittest:Test output:
I am not sure however if
pytest-asynciois working as intended.as @asvetlov said, unittest style doesn’t make use of @pytest.fixture at all.
just try to add one and you see it doesn’t inject it.