Skip to content

Commit cfc7336

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

File tree

11 files changed

+97
-8
lines changed

11 files changed

+97
-8
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ Example:
343343
_tests/test_myunit.py_
344344

345345
```py
346-
from unittest import TestCase
346+
from unittesting import TestCase
347347
348348
class MyTestCase(TestCase):
349349
@@ -448,6 +448,28 @@ 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+
452+
### Asyncio testing
453+
454+
Tests for `asyncio` are written using `IsolatedAsyncioTestCase` class.
455+
456+
457+
```py
458+
import asyncio
459+
460+
from unittesting import IsolatedAsyncioTestCase
461+
462+
async def a_coro():
463+
return 1 + 1
464+
465+
class MyAsyncTestCase(IsolatedAsyncioTestCase):
466+
async def test_something(self):
467+
result = await a_coro()
468+
await asyncio.sleep(1)
469+
self.assertEqual(result, 2)
470+
```
471+
472+
451473
## Helper TestCases
452474

453475
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/_Asyncio/.python-version

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

tests/_Asyncio/unittesting.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"deferred": false
3+
}

tests/test_3141596.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import shutil
44
import sublime
5+
import sys
56

67
from functools import wraps
78
from unittest import skipIf
@@ -10,6 +11,7 @@
1011
from unittesting import DeferrableTestCase
1112

1213
BASEDIR = os.path.dirname(os.path.abspath(__file__))
14+
PY33 = sys.version_info[:2] == (3, 3)
1315

1416

1517
def setup_package(package):
@@ -120,7 +122,7 @@ def assertOk(self, txt, msg=None):
120122

121123
class TestUnitTesting(UnitTestingTestCase):
122124
fixtures = (
123-
"_Success", "_Failure", "_Empty", "_Output", "_Deferred", "_Async"
125+
"_Success", "_Failure", "_Empty", "_Output", "_Deferred", "_Async", "_Asyncio"
124126
)
125127

126128
@with_package("_Success")
@@ -151,6 +153,11 @@ def test_deferred(self, txt):
151153
def test_async(self, txt):
152154
self.assertOk(txt)
153155

156+
@skipIf(PY33, "not applicable in Python 3.3")
157+
@with_package("_Asyncio")
158+
def test_asyncio(self, txt):
159+
self.assertOk(txt)
160+
154161

155162
class TestSyntax(UnitTestingTestCase):
156163
fixtures = (

unittesting/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from .core import AWAIT_WORKER
22
from .core import DeferrableMethod
33
from .core import DeferrableTestCase
4+
from .core import IsolatedAsyncioTestCase
5+
from .core import TestCase
46
from .core import expectedFailure
57
from .helpers import DeferrableViewTestCase
68
from .helpers import OverridePreferencesTestCase
@@ -13,10 +15,12 @@
1315
"AWAIT_WORKER",
1416
"DeferrableMethod",
1517
"DeferrableTestCase",
16-
"expectedFailure",
17-
"run_scheduler",
1818
"DeferrableViewTestCase",
19+
"expectedFailure",
20+
"IsolatedAsyncioTestCase",
1921
"OverridePreferencesTestCase",
22+
"run_scheduler",
2023
"TempDirectoryTestCase",
24+
"TestCase",
2125
"ViewTestCase",
2226
]

unittesting/core/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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
7+
from .py313.case import TestCase
68
from .py313.case import expectedFailure
79
from .py313.loader import DeferrableTestLoader
810
from .py313.runner import AWAIT_WORKER
@@ -11,6 +13,8 @@
1113
elif sys.version_info[:2] == (3, 8):
1214
from .py38.case import DeferrableMethod
1315
from .py38.case import DeferrableTestCase
16+
from .py38.case import IsolatedAsyncioTestCase
17+
from .py38.case import TestCase
1418
from .py38.case import expectedFailure
1519
from .py38.loader import DeferrableTestLoader
1620
from .py38.runner import AWAIT_WORKER
@@ -19,6 +23,8 @@
1923
elif sys.version_info[:2] == (3, 3):
2024
from .py33.case import DeferrableMethod
2125
from .py33.case import DeferrableTestCase
26+
from .py33.case import IsolatedAsyncioTestCase
27+
from .py33.case import TestCase
2228
from .py33.case import expectedFailure
2329
from .py33.loader import DeferrableTestLoader
2430
from .py33.runner import AWAIT_WORKER
@@ -34,5 +40,7 @@
3440
"DeferrableTestLoader",
3541
"DeferrableTestSuite",
3642
"DeferringTextTestRunner",
43+
"IsolatedAsyncioTestCase",
44+
"TestCase",
3745
"expectedFailure",
3846
]

unittesting/core/py313/case.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
import sys
33

44
from collections.abc import Generator as DeferrableMethod
5+
from unittest import IsolatedAsyncioTestCase
56
from unittest import TestCase
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+
"TestCase",
18+
"expectedFailure",
19+
]
1320

1421

1522
class DeferrableTestCase(TestCase):

unittesting/core/py33/case.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212

1313
from .runner import defer
1414

15-
__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"]
15+
__all__ = [
16+
"DeferrableMethod",
17+
"DeferrableTestCase",
18+
"IsolatedAsyncioTestCase",
19+
"TestCase",
20+
"expectedFailure",
21+
]
1622

1723

1824
def expectedFailure(func):
@@ -29,6 +35,11 @@ def wrapper(*args, **kwargs):
2935
return wrapper
3036

3137

38+
class IsolatedAsyncioTestCase:
39+
def __init__(self, *args, **kwargs):
40+
raise RuntimeError("Asyncio not supported by python 3.3!")
41+
42+
3243
class DeferrableTestCase(TestCase):
3344

3445
def _executeTestPart(self, function, outcome, isTest=False):

0 commit comments

Comments
 (0)