diff --git a/README.md b/README.md index aa646afe..03fb5170 100644 --- a/README.md +++ b/README.md @@ -343,7 +343,7 @@ Example: _tests/test_myunit.py_ ```py -from unittest import TestCase +from unittesting import TestCase class MyTestCase(TestCase): @@ -448,6 +448,28 @@ class TestCondition(DeferrableTestCase): see also [tests/test_defer.py](https://github.com/randy3k/UnitTesting-example/blob/master/tests/test_defer.py). + +### Asyncio testing + +Tests for `asyncio` are written using `IsolatedAsyncioTestCase` class. + + +```py +import asyncio + +from unittesting import IsolatedAsyncioTestCase + +async def a_coro(): + return 1 + 1 + +class MyAsyncTestCase(IsolatedAsyncioTestCase): + async def test_something(self): + result = await a_coro() + await asyncio.sleep(1) + self.assertEqual(result, 2) +``` + + ## Helper TestCases UnitTesting provides some helper test case classes, diff --git a/tests/_Asyncio/.python-version b/tests/_Asyncio/.python-version new file mode 100644 index 00000000..98fccd6d --- /dev/null +++ b/tests/_Asyncio/.python-version @@ -0,0 +1 @@ +3.8 \ No newline at end of file diff --git a/tests/_Asyncio/tests/test_coroutine.py b/tests/_Asyncio/tests/test_coroutine.py new file mode 100644 index 00000000..e26d2b16 --- /dev/null +++ b/tests/_Asyncio/tests/test_coroutine.py @@ -0,0 +1,15 @@ +import asyncio + +from unittest import skipIf +from unittesting import IsolatedAsyncioTestCase + + +async def a_coro(): + return 1 + 1 + + +class MyAsyncTestCase(IsolatedAsyncioTestCase): + async def test_something(self): + result = await a_coro() + await asyncio.sleep(1) + self.assertEqual(result, 2) diff --git a/tests/_Asyncio/unittesting.json b/tests/_Asyncio/unittesting.json new file mode 100644 index 00000000..8968d3ef --- /dev/null +++ b/tests/_Asyncio/unittesting.json @@ -0,0 +1,3 @@ +{ + "deferred": false +} diff --git a/tests/test_3141596.py b/tests/test_3141596.py index 4a081be3..8238faac 100644 --- a/tests/test_3141596.py +++ b/tests/test_3141596.py @@ -2,6 +2,7 @@ import re import shutil import sublime +import sys from functools import wraps from unittest import skipIf @@ -10,6 +11,7 @@ from unittesting import DeferrableTestCase BASEDIR = os.path.dirname(os.path.abspath(__file__)) +PY33 = sys.version_info[:2] == (3, 3) def setup_package(package): @@ -120,7 +122,7 @@ def assertOk(self, txt, msg=None): class TestUnitTesting(UnitTestingTestCase): fixtures = ( - "_Success", "_Failure", "_Empty", "_Output", "_Deferred", "_Async" + "_Success", "_Failure", "_Empty", "_Output", "_Deferred", "_Async", "_Asyncio" ) @with_package("_Success") @@ -151,6 +153,11 @@ def test_deferred(self, txt): def test_async(self, txt): self.assertOk(txt) + @skipIf(PY33, "not applicable in Python 3.3") + @with_package("_Asyncio") + def test_asyncio(self, txt): + self.assertOk(txt) + class TestSyntax(UnitTestingTestCase): fixtures = ( diff --git a/unittesting/__init__.py b/unittesting/__init__.py index e111fd1a..dfd33448 100644 --- a/unittesting/__init__.py +++ b/unittesting/__init__.py @@ -1,6 +1,8 @@ from .core import AWAIT_WORKER from .core import DeferrableMethod from .core import DeferrableTestCase +from .core import IsolatedAsyncioTestCase +from .core import TestCase from .core import expectedFailure from .helpers import DeferrableViewTestCase from .helpers import OverridePreferencesTestCase @@ -13,10 +15,12 @@ "AWAIT_WORKER", "DeferrableMethod", "DeferrableTestCase", - "expectedFailure", - "run_scheduler", "DeferrableViewTestCase", + "expectedFailure", + "IsolatedAsyncioTestCase", "OverridePreferencesTestCase", + "run_scheduler", "TempDirectoryTestCase", + "TestCase", "ViewTestCase", ] diff --git a/unittesting/core/__init__.py b/unittesting/core/__init__.py index 34ef1b24..a5f4541f 100644 --- a/unittesting/core/__init__.py +++ b/unittesting/core/__init__.py @@ -3,6 +3,8 @@ if sys.version_info[:2] >= (3, 13): from .py313.case import DeferrableMethod from .py313.case import DeferrableTestCase + from .py313.case import IsolatedAsyncioTestCase + from .py313.case import TestCase from .py313.case import expectedFailure from .py313.loader import DeferrableTestLoader from .py313.runner import AWAIT_WORKER @@ -11,6 +13,8 @@ elif sys.version_info[:2] == (3, 8): from .py38.case import DeferrableMethod from .py38.case import DeferrableTestCase + from .py38.case import IsolatedAsyncioTestCase + from .py38.case import TestCase from .py38.case import expectedFailure from .py38.loader import DeferrableTestLoader from .py38.runner import AWAIT_WORKER @@ -19,6 +23,8 @@ elif sys.version_info[:2] == (3, 3): from .py33.case import DeferrableMethod from .py33.case import DeferrableTestCase + from .py33.case import IsolatedAsyncioTestCase + from .py33.case import TestCase from .py33.case import expectedFailure from .py33.loader import DeferrableTestLoader from .py33.runner import AWAIT_WORKER @@ -34,5 +40,7 @@ "DeferrableTestLoader", "DeferrableTestSuite", "DeferringTextTestRunner", + "IsolatedAsyncioTestCase", + "TestCase", "expectedFailure", ] diff --git a/unittesting/core/py313/case.py b/unittesting/core/py313/case.py index f147865d..1d3dfc1d 100644 --- a/unittesting/core/py313/case.py +++ b/unittesting/core/py313/case.py @@ -2,6 +2,7 @@ import sys from collections.abc import Generator as DeferrableMethod +from unittest import IsolatedAsyncioTestCase from unittest import TestCase from unittest.case import _addSkip from unittest.case import _Outcome @@ -9,7 +10,13 @@ from .runner import defer -__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"] +__all__ = [ + "DeferrableMethod", + "DeferrableTestCase", + "IsolatedAsyncioTestCase", + "TestCase", + "expectedFailure", +] class DeferrableTestCase(TestCase): diff --git a/unittesting/core/py33/case.py b/unittesting/core/py33/case.py index d20af724..f55555cd 100644 --- a/unittesting/core/py33/case.py +++ b/unittesting/core/py33/case.py @@ -12,7 +12,13 @@ from .runner import defer -__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"] +__all__ = [ + "DeferrableMethod", + "DeferrableTestCase", + "IsolatedAsyncioTestCase", + "TestCase", + "expectedFailure", +] def expectedFailure(func): @@ -29,6 +35,11 @@ def wrapper(*args, **kwargs): return wrapper +class IsolatedAsyncioTestCase: + def __init__(self, *args, **kwargs): + raise RuntimeError("Asyncio not supported by python 3.3!") + + class DeferrableTestCase(TestCase): def _executeTestPart(self, function, outcome, isTest=False): diff --git a/unittesting/core/py38/case.py b/unittesting/core/py38/case.py index 48fcb712..47c02577 100644 --- a/unittesting/core/py38/case.py +++ b/unittesting/core/py38/case.py @@ -2,12 +2,19 @@ from collections.abc import Generator as DeferrableMethod from unittest import TestCase +from unittest import IsolatedAsyncioTestCase from unittest.case import _Outcome from unittest.case import expectedFailure from .runner import defer -__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"] +__all__ = [ + "DeferrableMethod", + "DeferrableTestCase", + "IsolatedAsyncioTestCase", + "TestCase", + "expectedFailure", +] class DeferrableTestCase(TestCase):