simple way of using pytest with async unit tests
Project description
asyncnostic
Using pytest to run unit-tests that now need to run async? This package serves as a catch-all for mixing regular and asynchronous tests.
import unittest
import asyncio
from asyncnostic import asyncnostic
# if your test class has no async components to it at all
# asyncnostic will stay out of your way
@asyncnostic
class TestAThing(unittest.TestCase):
# the kwarg "loop" as an method argument is optional
# If you don't want it, leave it out.
# The event loop will be the same for all tests
# we keep the same setUp and tearDown naming conventions from unittest
async def setUp(self, loop):
self.device = Device()
await self.device.start(loop)
# as long as your tests start with convention - starting in "test"
# asyncnostic will pick them up
async def test_async_simple(self, loop):
result = await self.device.property
assert result
# we even handle regular tests thrown into the mix
# you can even ask for the loop if you want
def test_regular_simple(self, loop):
assert self.device.loop == loop
How it works under the hood
We find all coroutines that start with "test", as is convention. These tests are then transformed into regular methods, and the asynchronous test is overwritten by the regular test method with the same name. Tests that are not asynchronous are left unchanged. If any of the tests request a loop, the event loop (unique per class) will be passed as a keyword argument.
Contributing
Feel free to open a PR or and issue if there's any problem with Asyncnostic
! If you do open a PR, make sure to have run nox
before review :)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for asyncnostic-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c79432ff0303ad7532f5e808a0f4914459b5dbe10b540da1bc47d53a4f36960f |
|
MD5 | d91769478c1f22e305d8665461ab119f |
|
BLAKE2b-256 | 9edd15c5cf718b77117995da153e669efcb805ed9735b4729e1e7746a7655b40 |