Skip to content

Commit 0a55014

Browse files
committed
Add basic support for asyncio test cases
1 parent 2e3b004 commit 0a55014

File tree

7 files changed

+60
-5
lines changed

7 files changed

+60
-5
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,27 @@ class TestCondition(DeferrableTestCase):
448448

449449
see also [tests/test_defer.py](https://github.com/randy3k/UnitTesting-example/blob/master/tests/test_defer.py).
450450

451+
### Asyncio testing
452+
453+
Tests for `asyncio` are written using `IsolatedAsyncioTestCase` class.
454+
455+
456+
```py
457+
import asyncio
458+
459+
from unittesting import IsolatedAsyncioTestCase
460+
461+
async def a_coro():
462+
return 1 + 1
463+
464+
class MyAsyncTestCase(IsolatedAsyncioTestCase):
465+
async def test_something(self):
466+
result = await a_coro()
467+
await asyncio.sleep(1)
468+
self.assertEqual(result, 2)
469+
```
470+
471+
451472
## Helper TestCases
452473

453474
UnitTesting provides some helper test case classes,

dependencies.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"*": {
3-
">3000": [
3+
"3000 - 3999": [
44
"coverage"
5+
],
6+
">4000": [
7+
"coverage",
8+
"sublime_aio"
59
]
610
}
711
}

tests/test_asyncio.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import asyncio
2+
3+
from unittesting import IsolatedAsyncioTestCase
4+
5+
6+
async def a_coro():
7+
return 1 + 1
8+
9+
class MyAsyncTestCase(IsolatedAsyncioTestCase):
10+
async def test_something(self):
11+
result = await a_coro()
12+
await asyncio.sleep(1)
13+
self.assertEqual(result, 2)

unittesting/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .core import IsolatedAsyncioTestCase
12
from .core import AWAIT_WORKER
23
from .core import DeferrableMethod
34
from .core import DeferrableTestCase
@@ -13,10 +14,11 @@
1314
"AWAIT_WORKER",
1415
"DeferrableMethod",
1516
"DeferrableTestCase",
16-
"expectedFailure",
17-
"run_scheduler",
1817
"DeferrableViewTestCase",
18+
"expectedFailure",
19+
"IsolatedAsyncioTestCase",
1920
"OverridePreferencesTestCase",
21+
"run_scheduler",
2022
"TempDirectoryTestCase",
2123
"ViewTestCase",
2224
]

unittesting/core/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
if sys.version_info[:2] >= (3, 13):
44
from .py313.case import DeferrableMethod
55
from .py313.case import DeferrableTestCase
6+
from .py313.case import IsolatedAsyncioTestCase
67
from .py313.case import expectedFailure
78
from .py313.loader import DeferrableTestLoader
89
from .py313.runner import AWAIT_WORKER
@@ -11,6 +12,7 @@
1112
elif sys.version_info[:2] == (3, 8):
1213
from .py38.case import DeferrableMethod
1314
from .py38.case import DeferrableTestCase
15+
from .py38.case import IsolatedAsyncioTestCase
1416
from .py38.case import expectedFailure
1517
from .py38.loader import DeferrableTestLoader
1618
from .py38.runner import AWAIT_WORKER
@@ -34,5 +36,6 @@
3436
"DeferrableTestLoader",
3537
"DeferrableTestSuite",
3638
"DeferringTextTestRunner",
39+
"IsolatedAsyncioTestCase",
3740
"expectedFailure",
3841
]

unittesting/core/py313/case.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
from collections.abc import Generator as DeferrableMethod
55
from unittest import TestCase
6+
from unittest import IsolatedAsyncioTestCase
67
from unittest.case import _addSkip
78
from unittest.case import _Outcome
89
from unittest.case import expectedFailure
910

1011
from .runner import defer
1112

12-
__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"]
13+
__all__ = [
14+
"DeferrableMethod",
15+
"DeferrableTestCase",
16+
"IsolatedAsyncioTestCase",
17+
"expectedFailure",
18+
]
1319

1420

1521
class DeferrableTestCase(TestCase):

unittesting/core/py38/case.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
from collections.abc import Generator as DeferrableMethod
44
from unittest import TestCase
5+
from unittest import IsolatedAsyncioTestCase
56
from unittest.case import _Outcome
67
from unittest.case import expectedFailure
78

89
from .runner import defer
910

10-
__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"]
11+
__all__ = [
12+
"DeferrableMethod",
13+
"DeferrableTestCase",
14+
"IsolatedAsyncioTestCase",
15+
"expectedFailure",
16+
]
1117

1218

1319
class DeferrableTestCase(TestCase):

0 commit comments

Comments
 (0)