If your code needs to work the same way with regular, asynchronous, and generator functions, you normally have to write three sets of nearly identical tests. With this library, you no longer need to do that — just use the special pytest fixture, which automatically generates the variants.
To use the fixture, install it via:
pip install transtestsThe transformed fixture is now available. It returns a decorator that transforms the original function into one of three variants: a synchronous function, an asynchronous function, or a generator function:
from asyncio import run
from inspect import iscoroutinefunction, isgeneratorfunction
def test_something(transformed):
@transformed
def some_function(a, b):
return a + b
if iscoroutinefunction(some_function):
assert run(some_function(1, 2)) == 3
elif isgeneratorfunction(some_function):
assert list(some_function(1, 2)) == [3]
else:
assert some_function(1, 2) == 3↑ The test will be run three times automatically, once per variant.
This functionality is based on the transfunctions library, so you can use context managers from that library in the source function.