pytest-contextfixture makes it possible to define pytest fixtures as context managers.
A contextfixture works like a standard fixture, but it allows the definition to be written as a generator. This simplifies the teardown code and allows for other context managers to be used within a fixture.
Installation
pip install pytest-contextfixture
Usage
Consider this example, using the standard pytest.fixture:
import pytest
@pytest.fixture
def dependency(request)
def teardown():
# fixture teardown code goes here
request.addfinalizer(teardown)
return 1234
def test_foo(dependency):
assert fn_under_test(dependency) == 'expected'
With pytest.contextfixture, this is equivalent:
import pytest
@pytest.contextfixture
def dependency(request):
# fixture setup code goes here
yield 1234
# fixture teardown code goes here
def test_foo(dependency):
assert fn_under_test(dependency) == 'expected'
While this is a slightly nicer syntax, when using other context managers to get a dependency for a fixture, this becomes more useful:
@pytest_contextfixture
def dependency(request):
with setup_something():
with setup_something_else() as d:
yield d
def test_foo(dependency):
assert fn_under_test(dependency) == 'expected'
test_foo will then run in the context of setup_something and setup_something_else.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pytest-contextfixture-0.1.1.tar.gz.
File metadata
- Download URL: pytest-contextfixture-0.1.1.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b55e3875c1240c9bff5fb3f93396847692257617e7efc58845f907717278f26
|
|
| MD5 |
c6674795b6f0bca117622cd909aa4b35
|
|
| BLAKE2b-256 |
7bafac35f60dfd9203afb0f6bd80e91fb409eb92c7bf1c6b49601928128640c4
|