Reputation: 3320
I am using asynctest and I want to leverage pytest fixture. I'm intersted by (but not only) the caplog fixture.
In an asynctest testing class, I can use with self.assertLogs():
but this is not sufficient when my logs are not produced within the block.
import asyncio
import logging
import asynctest
async def worker():
logging.error(":(")
class TestClass(asynctest.TestCase):
"""Testing class. """
async def setUp(self):
self.worker_task = asyncio.ensure_future(worker())
async def tearDown(self):
try:
self.worker_task.cancel()
except asyncio.CancelledError:
pass
async def test_get_error_log(self):
""" Simple test that assert logs emitted. """
with self.assertLogs(level="ERROR") as cm:
await asyncio.sleep(1)
The test above if failing but if I have:
---------- Captured log call -----------
ERROR root:tmp.py:7 :(
Upvotes: 2
Views: 632
Reputation: 3320
In fact asynctest is build on top of unittest and has nothing in common with the pytest testing framework.
So my own response is: for pytest fixtures, then use pytest instead of asynctest.
For the logging issue, it can be solved by getting and mocking the logging function.
async def test_get_error_log(self):
""" Simple test that assert logs emitted. """
logging.error = asynctest.Mock()
await asyncio.sleep(1)
logging.error.assert_called_once_with(":(")
Upvotes: 1