Skip to main content

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.

Metadata

Release files for pytest-contextfixture 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-contextfixture 0.1.1
File Size Uploaded Provenance
pytest-contextfixture-0.1.1.tar.gz 3.2 kB Without provenance Details

Release files / pytest-contextfixture-0.1.1.tar.gz

Download URL pytest-contextfixture-0.1.1.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0b55e3875c1240c9bff5fb3f93396847692257617e7efc58845f907717278f26
BLAKE2b-256 checksum
How to use checksums
7bafac35f60dfd9203afb0f6bd80e91fb409eb92c7bf1c6b49601928128640c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pytest-contextfixture 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page