Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pelme/pytest-contextfixture
https://github.com/pelme/pytest-contextfixture
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/pelme/pytest-contextfixture
- Owner: pelme
- License: other
- Archived: true
- Created: 2013-02-02T09:56:14.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-10-02T11:30:25.000Z (about 10 years ago)
- Last Synced: 2024-04-24T02:01:08.397Z (7 months ago)
- Language: Python
- Size: 156 KB
- Stars: 3
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
This repository is no longer maintained, please see the yield_fixture functionality in pytest: http://pytest.org/latest/yieldfixture.html
~~~~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 heredef 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 ddef test_foo(dependency):
assert fn_under_test(dependency) == 'expected'test_foo will then run in the context of setup_something and
setup_something_else.