Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ Example:
_tests/test_myunit.py_

```py
from unittest import TestCase
from unittesting import TestCase

class MyTestCase(TestCase):

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/_Asyncio/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.8
15 changes: 15 additions & 0 deletions tests/_Asyncio/tests/test_coroutine.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions tests/_Asyncio/unittesting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deferred": false
}
9 changes: 8 additions & 1 deletion tests/test_3141596.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import shutil
import sublime
import sys

from functools import wraps
from unittest import skipIf
Expand All @@ -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):
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 = (
Expand Down
8 changes: 6 additions & 2 deletions unittesting/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,10 +15,12 @@
"AWAIT_WORKER",
"DeferrableMethod",
"DeferrableTestCase",
"expectedFailure",
"run_scheduler",
"DeferrableViewTestCase",
"expectedFailure",
"IsolatedAsyncioTestCase",
"OverridePreferencesTestCase",
"run_scheduler",
"TempDirectoryTestCase",
"TestCase",
"ViewTestCase",
]
8 changes: 8 additions & 0 deletions unittesting/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -34,5 +40,7 @@
"DeferrableTestLoader",
"DeferrableTestSuite",
"DeferringTextTestRunner",
"IsolatedAsyncioTestCase",
"TestCase",
"expectedFailure",
]
9 changes: 8 additions & 1 deletion unittesting/core/py313/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
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
from unittest.case import expectedFailure

from .runner import defer

__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"]
__all__ = [
"DeferrableMethod",
"DeferrableTestCase",
"IsolatedAsyncioTestCase",
"TestCase",
"expectedFailure",
]


class DeferrableTestCase(TestCase):
Expand Down
13 changes: 12 additions & 1 deletion unittesting/core/py33/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

from .runner import defer

__all__ = ["DeferrableMethod", "DeferrableTestCase", "expectedFailure"]
__all__ = [
"DeferrableMethod",
"DeferrableTestCase",
"IsolatedAsyncioTestCase",
"TestCase",
"expectedFailure",
]


def expectedFailure(func):
Expand All @@ -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):
Expand Down
9 changes: 8 additions & 1 deletion unittesting/core/py38/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down