base.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. import asyncio
  3. import sys
  4. try:
  5. # as from Py3.8 unittest supports coroutines as test functions
  6. from unittest import IsolatedAsyncioTestCase, skipIf
  7. def fail_on(**kw): # noqa
  8. def outer(fn):
  9. def inner(*args, **kwargs):
  10. return fn(*args, **kwargs)
  11. return inner
  12. return outer
  13. except ImportError:
  14. # fallback to asynctest
  15. from asynctest import fail_on, skipIf
  16. from asynctest.case import TestCase as IsolatedAsyncioTestCase
  17. IS_GTE_PY38 = sys.version_info >= (3, 8)
  18. class AsyncTestCase(IsolatedAsyncioTestCase):
  19. """Asynchronous test case class that covers up differences in usage
  20. between unittest (starting from Python 3.8) and asynctest.
  21. `setup` and `teardown` is used to be called before each test case
  22. (note: that they are in lowercase)
  23. """
  24. async def setup(self):
  25. pass
  26. async def teardown(self):
  27. pass
  28. if IS_GTE_PY38:
  29. # from Python3.8
  30. async def asyncSetUp(self):
  31. self.loop = asyncio.get_event_loop()
  32. await self.setup()
  33. async def asyncTearDown(self):
  34. await self.teardown()
  35. else:
  36. # asynctest
  37. use_default_loop = False
  38. async def setUp(self) -> None:
  39. await self.setup()
  40. async def tearDown(self) -> None:
  41. await self.teardown()