In python, in my subclass of unittest.TestCase, can I use both 'def' and 'async def' for…
- User
In python, in my subclass of unittest.TestCase, can I use both 'def' and 'async def' for my test functions?
Assistant
Yes, you can have both regular (
def) and asynchronous (async def) test methods in the sameunittest.TestCasesubclass, but with some important caveats:- Regular
def test_*methods will be discovered and run normally async def test_*methods in a regularTestCasewon't be automatically discovered or executed by the default test runner
For proper async test support, you should use
unittest.IsolatedAsyncioTestCase(available since Python 3.8):import unittest class MyTests(unittest.TestCase): def test_sync(self): self.assertEqual(1, 1) async def helper_async(self): # Not run as a test return 42 class MyAsyncTests(unittest.IsolatedAsyncioTestCase): async def test_async(self): self.assertEqual(await self.helper_async(), 42) async def helper_async(self): return 42- Regular
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.