API¶
Register fixtures to be used automatically when entering a scope. |
|
Create a new test case to be used with parametrized tests. |
|
A decorator to create a new fixture. |
|
Create a test for each case, each test calling the given func. |
|
Scope for which the fixture. |
Modules¶
Public¶
- pytypest.autouse(*fixtures: Fixture[[], None]) None ¶
Register fixtures to be used automatically when entering a scope.
Can be called only once in runtime.
autouse( create_database, clear_cache, fixtures.forbid_networking, )
- pytypest.case()¶
Create a new test case to be used with parametrized tests.
def _test_add(a: int, b: int, exp: int): assert a + b == exp test_add = parametrize( _test_add, case(4, 5, exp=9), )
- pytypest.fixture(callback: None = None, *, scope: Literal[Scope.FUNCTION] = Scope.FUNCTION) FixtureMaker ¶
- pytypest.fixture(callback: None = None, *, scope: Scope) FixtureMakerWithScope
- pytypest.fixture(callback: Callable[P, Iterator[R]]) Fixture[P, R]
- pytypest.fixture(callback: Callable[P, R]) Fixture[P, R]
A decorator to create a new fixture.
Fixtures are executed only when called, cached for the given scope, and may have teardown logic that is executed when exiting the scope.
@fixture def get_user() -> Iterator[User]: # setup u = User() # fixtures can use other fixtures db = get_database() db.insert(u) # provide data for the test yield u # teardown db.delete(u)
You can call the fixture to get the yielded value:
def test_user(): user = get_user()
Or you can use it as a context manager:
def test_user(): with get_user as user: ...
Fixtures can accept arguments:
@fixture def get_user(name: str): ... def test_user(): conn = get_user(name='Guido')
Fixtures without teardown may use return instead of yield:
@fixture def get_user() -> User: return User()
Fixtures can be called not only from test functions, but from other fixtures, pytest fixtures, or helper functions within a test run.
- pytypest.parametrize(func: Callable[[P], None], *cases: Case[P], **named_cases: Case[P]) Callable[[], None] ¶
Create a test for each case, each test calling the given func.
def _test_add(a: int, b: int, exp: int) -> None: assert a + b == exp test_add = parametrize( _test_add, case(3, 4, exp=7), case(4, 5, exp=9), zeros=case(0, 0, exp=0), )
- class pytypest.Scope¶
Scope for which the fixture.
The scope defines when the fixture cache will be reset and the teardown executed.
- FUNCTION = 'function'¶
Default. Teardown is called at the end of the test function.
- CLASS = 'class'¶
Teardown is called after the last test in a test class.
- MODULE = 'module'¶
Teardown is called after the last test in a file.
- PACKAGE = 'package'¶
Experimental. Teardown is called after the last test in a directory.
- SESSION = 'session'¶
Teardown is called after the last tests overall in the current session.
Private¶
- class pytypest._fixture.Fixture¶
A test fixture with setup and optional teardown.
Should be constructed using
pytypest.fixture()
:@fixture def get_user() -> Iterator[User]: ... # setup yield User() ... # teardown
- __call__(*args: P.args, **kwargs: P.kwargs) R ¶
Allows the fixture to be called as a function.
@fixture def get_user(): ... user = get_user()
- setup(*args: P.args, **kwargs: P.kwargs) R ¶
Execute setup logic of the fixture and get its result.
Setup is everything that goes before yield or return.
Avoid using this method directly. It doesn’t use cached results, doesn’t use the scope, and doesn’t defer teardown. Prefer calling the fixture or using it as a context manager.
- teardown() None ¶
Execute teardown logic of the fixture (if available).
Teardown is the code that goes after yield (if yield is present).
Can be safely called mutiple times.
- __enter__() R ¶
Allows the fixture to be used as a context manager.
@fixture def get_user(): ... with get_user as user: ...
Regardless of the scope, the setup is executed when entering the context, and the teardown is when leaving it.
- class pytypest._case.CaseMaker¶
Create a new test case to be used with parametrized tests.
def _test_add(a: int, b: int, exp: int): assert a + b == exp test_add = parametrize( _test_add, case(4, 5, exp=9), )
- class pytypest._case.Case¶
A single test case for parametrized tests.
Use
pytypest.case()
to create a new one.