- commit
- b245d6863fbc5974dd20b420baf75c812e6a646b
- parent
- bcca4f5657d04d5a5e88760ab8ce07e8d7ec8311
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2019-02-09 16:03
handle async tests automatically
Diffstat
| M | README.md | 20 | ++------------------ |
| M | assamtest/decorators.py | 12 | ------------ |
| M | assamtest/register.py | 11 | ++++++++++- |
| M | tests.py | 27 | +++++++++++++++------------ |
4 files changed, 27 insertions, 43 deletions
diff --git a/README.md b/README.md
@@ -67,6 +67,8 @@ Register a function as a test: 67 67 * `args` (list): Arguments that should be passed to the test function 68 68 * `decorators` (list): The test function will be passed through these decorators before being executed 69 69 -1 70 Async functions are automatically executed in an event loop. -1 71 70 72 ```python 71 73 import assamtest 72 74 from assamtest import expect @@ -156,24 +158,6 @@ def my_test(value): 156 158 expect.equal(2 + 2, value) 157 159 ``` 158 160159 -1 ### `@decorator.synchronize`160 -1161 -1 Start an asyncio event loop for the test and wait for it to complete::162 -1163 -1 ```python164 -1 import asyncio165 -1166 -1 import assamtest167 -1 from assamtest import expect168 -1 from assamtest.decorators import synchronize169 -1170 -1 @assamtest.test()171 -1 @synchronize172 -1 async def my_test():173 -1 await asyncio.sleep(0.1)174 -1 expect.equal(2 + 2, 4)175 -1 ```176 -1177 161 ### `Outcome(err, status, level)` 178 162 179 163 Can be used to implement custom outcomes.
diff --git a/assamtest/decorators.py b/assamtest/decorators.py
@@ -1,4 +1,3 @@1 -1 import asyncio2 1 import functools 3 2 4 3 from .expect import expect @@ -18,14 +17,3 @@ def skip(fn): 18 17 def wrapper(*args, **kwargs): 19 18 raise Outcome(None, 'skipped', 'INFO') 20 19 return wrapper21 -122 -123 -1 def synchronize(fn):24 -1 @functools.wraps(fn)25 -1 def wrapper(*args, **kwargs):26 -1 coro = asyncio.coroutine(fn)27 -1 future = coro(*args, **kwargs)28 -1 loop = asyncio.get_event_loop()29 -1 loop.run_until_complete(future)30 -1 loop.close()31 -1 return wrapper
diff --git a/assamtest/register.py b/assamtest/register.py
@@ -1,3 +1,7 @@ -1 1 import asyncio -1 2 import functools -1 3 import inspect -1 4 1 5 stack = [{'tests': [], 'suites': []}] 2 6 3 7 @@ -16,8 +20,13 @@ def _prepare(name, args, decorators, fn): 16 20 if args: 17 21 _name += ':' + ';'.join(str(arg) for arg in args) 18 22 -1 23 @functools.wraps(fn) 19 24 def wrapper():20 -1 fn(*args)-1 25 result = fn(*args) -1 26 if inspect.isawaitable(result): -1 27 loop = asyncio.get_event_loop() -1 28 loop.run_until_complete(result) -1 29 loop.close() 21 30 22 31 for d in decorators: 23 32 wrapper = d(wrapper)
diff --git a/tests.py b/tests.py
@@ -44,6 +44,19 @@ def test_simple(): 44 44 45 45 46 46 @assamtest.test() -1 47 def test_async(): -1 48 import asyncio -1 49 -1 50 @assamtest.test(args=[4]) -1 51 @assamtest.test(args=[5], decorators=[_decorators.fail]) -1 52 async def my_test(value): -1 53 await asyncio.sleep(0.1) -1 54 expect.equal(2 + 2, value) -1 55 -1 56 expect_stats(passed=2) -1 57 -1 58 -1 59 @assamtest.test() 47 60 def suite_simple(): 48 61 @assamtest.suite() 49 62 def my_suite(): @@ -57,6 +70,7 @@ def suite_simple(): 57 70 58 71 expect_stats(passed=1) 59 72 -1 73 60 74 @assamtest.test() 61 75 def suite_nonlocal(): 62 76 @assamtest.suite() @@ -79,6 +93,7 @@ def suite_nonlocal(): 79 93 80 94 expect_stats(passed=3) 81 95 -1 96 82 97 @assamtest.test() 83 98 def expect_simple(): 84 99 expect.equal(2 + 2, 4) @@ -106,15 +121,3 @@ def decorators(): 106 121 expect.equal(2 + 2, value) 107 122 108 123 expect_stats(passed=2)109 -1110 -1 @assamtest.test()111 -1 def synchronize():112 -1 import asyncio113 -1114 -1 @assamtest.test()115 -1 @_decorators.synchronize116 -1 async def my_test():117 -1 await asyncio.sleep(0.1)118 -1 expect.equal(2 + 2, 4)119 -1120 -1 expect_stats(passed=1)