Pytest fixtures with conventional import semantics
Project description
pytest-unmagic
Pytest fixtures with conventional import semantics.
pytest's fixtures are powerful, but they rely on "magic": a test requests a
fixture by adding an argument whose name matches the fixture, and pytest
resolves it at runtime by searching conftest.py files and plugins. Where a
fixture comes from is not visible in the code, "go to definition" does not work,
and fixtures cannot be applied to unittest.TestCase tests.
pytest-unmagic applies the principle that explicit is better than implicit: fixtures are imported, applied with a decorator, and called like ordinary Python objects. Every dependency is visible and easy to navigate. Unmagic fixtures coexist with standard pytest fixtures, so a project can adopt them gradually, one fixture at a time.
Installation
pip install pytest-unmagic
Usage
Define fixtures with the unmagic.fixture decorator, and apply them to other
fixtures or test functions with unmagic.use.
from unmagic import fixture, use
traces = []
@fixture
def tracer():
assert not traces, f"unexpected traces before setup: {traces}"
yield
traces.clear()
@use(tracer)
def test_append():
traces.append("hello")
assert traces, "expected at least one trace"
A fixture must yield exactly once. The @use decorator causes the fixture to be
set up and registered for tear down, but does not pass the yielded value to the
decorated function. This is appropriate for fixtures that have side effects.
The location where a fixture is defined has no affect on where it can be used. Any code that can import it can use it as long as it is executed in the context of running tests and does not violate scope restrictions.
@use shorthand
If a single fixture is being applied to another fixture or test it may be
applied directly as a decorator without @use(). The test in the example above
could have been written as
@tracer
def test_append():
traces.append("hello")
assert traces, "expected at least one trace"
Call a fixture to retrieve its value
The value of a fixture can be retrieved within a test function or other fixture
by calling the fixture. This is similar to request.getfixturevalue().
@fixture
def tracer():
assert not traces, f"unexpected traces before setup: {traces}"
yield traces
traces.clear()
def test_append():
traces = tracer()
traces.append("hello")
assert traces, "expected at least one trace"
Learn more
The reference guide documents the rest of unmagic's features in detail, including:
- Applying fixtures to test classes and
unittest.TestCasetests - Fixture scopes and teardown
- Autouse fixtures
- Accessing pytest's request object and reusing
@pytest.fixturefixtures - Chaining fixtures that depend on other fixtures
- Erecting a fence to flag remaining magic-fixture usage
See CONTRIBUTING.md for how to run the test suite and publish a new release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_unmagic-1.1.0.tar.gz.
File metadata
- Download URL: pytest_unmagic-1.1.0.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdde56c72296c2269e4d9eec8cdc67c84c82a431bef0114457ec4800a72ad84c
|
|
| MD5 |
15e0814d7c39109d9efdc96cbbe62a3e
|
|
| BLAKE2b-256 |
c06c9e38b289b61686169ef3941f4f11958047ed055f34b54ac8f90e5901654d
|
Provenance
The following attestation bundles were made for pytest_unmagic-1.1.0.tar.gz:
Publisher:
pypi.yml on dimagi/pytest-unmagic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_unmagic-1.1.0.tar.gz -
Subject digest:
cdde56c72296c2269e4d9eec8cdc67c84c82a431bef0114457ec4800a72ad84c - Sigstore transparency entry: 1724611445
- Sigstore integration time:
-
Permalink:
dimagi/pytest-unmagic@03a5d2cd161b3da678986da214359fdee92a3c2b -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/dimagi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@03a5d2cd161b3da678986da214359fdee92a3c2b -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytest_unmagic-1.1.0-py3-none-any.whl.
File metadata
- Download URL: pytest_unmagic-1.1.0-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e98d551e9b165e1e8250299108e87a5f752a13c745f819df7a5f0ee9d4d4144
|
|
| MD5 |
0f62bdd213fd492995bbc77fb36d6fba
|
|
| BLAKE2b-256 |
24740ad96f4701284205fdefc4cefc711eb7ef593d559d13b4cbbf06b3969bd8
|
Provenance
The following attestation bundles were made for pytest_unmagic-1.1.0-py3-none-any.whl:
Publisher:
pypi.yml on dimagi/pytest-unmagic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_unmagic-1.1.0-py3-none-any.whl -
Subject digest:
8e98d551e9b165e1e8250299108e87a5f752a13c745f819df7a5f0ee9d4d4144 - Sigstore transparency entry: 1724611566
- Sigstore integration time:
-
Permalink:
dimagi/pytest-unmagic@03a5d2cd161b3da678986da214359fdee92a3c2b -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/dimagi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@03a5d2cd161b3da678986da214359fdee92a3c2b -
Trigger Event:
push
-
Statement type: