Skip to main content

pytest-autofixture

Use pytest fixtures by bare name. Tests and fixtures can reference any fixture without declaring it as a function parameter.

Install

pip install pytest-autofixture

Requires Python 3.11+ and pytest 8.4+. The plugin is active once installed; turn it off for a run with -p no:autofixture.

Usage

Any name in a test or fixture that would otherwise raise NameError is looked up as a fixture. Lookup is lazy, so a fixture is set up only if the code reaches it. Scope, overrides and teardown work exactly as they do for declared fixtures.

Fixtures resolve their dependencies from where the test is, so an inner class can override one value and outer fixtures pick it up, like a nested let in RSpec:

@pytest.fixture
def role():
    return "member"


@pytest.fixture
def user():
    return User(role=role)


def test_member():
    assert not user.can_delete()


class TestAdmin:
    @pytest.fixture
    def role(self):
        return "admin"

    def test_admin(self):
        assert user.can_delete()

Declared parameters still work and mix freely with bare names.

parametrize works the same way, no declaration needed:

@pytest.mark.parametrize("role", ["admin", "owner"])
def test_can_delete():
    assert user.can_delete()

let

For one-liners, let defines a fixture right where it's written, at module level or in a test class, like RSpec's let(:role) { "member" }:

from pytest_autofixture import let

let.role = "member"
let.user = lambda: User(role=role)


class TestAdmin:
    let.role = "admin"

    def test_admin(self):
        assert user.can_delete()

What you assign decides how it behaves:

  • A lambda is called on first use, at most once per test, so it can use other fixtures by bare name.
  • A list, dict, set or bytearray is deep-copied for each test, so let.items = [] starts empty every time. Use a lambda if the contents must stay the same objects.
  • Anything else is shared by every test. That's fine for strings, numbers and classes (let.model = User is the class, not an instance).

A builtin name such as let.id raises TypeError, since the builtin would always win. Use a regular fixture when you need a scope, params or teardown. There's no special subject: let.subject = lambda: Order(items=items) is just a fixture named subject.

Coming from RSpec

RSpec pytest-autofixture
let(:user) { User.new(role:) } let.user = lambda: User(role=role)
let! / before @pytest.fixture(autouse=True)
after a yield fixture
subject let.subject = lambda: ...
describe / context a nested class TestX
shared_examples a base test class
same examples with a different let @pytest.mark.parametrize("role", [...])

Limitations

Declare the fixture as a parameter instead when:

  • the test is wrapped by a decorator such as @mock.patch or hypothesis's @given (the test fails with an error saying so)
  • the fixture is parametrized
  • an async test uses an async fixture
  • a yield fixture first uses the name after its yield (pytest 9.1+ refuses fixture lookups during teardown)

Only test and fixture bodies resolve bare names. A module-level helper raises NameError for them, so pass the value in instead.

Linters flag bare fixture names as undefined (ruff F821, pyright). Silence that for test files, e.g. in pyproject.toml:

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["F821"]

installs

Metadata

Release files for pytest-autofixture 0.3.0

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-autofixture 0.3.0
File Size Uploaded Provenance Actions
pytest_autofixture-0.3.0.tar.gz 9.9 kB With provenance Details for pytest_autofixture-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-autofixture 0.3.0
File Interpreter ABI Platform Actions
pytest_autofixture-0.3.0-py3-none-any.whl Python 3 none any Details for pytest_autofixture-0.3.0-py3-none-any.whl

Total release size: 17.3 kB

Release files / pytest_autofixture-0.3.0.tar.gz

Download URL pytest_autofixture-0.3.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5f89679ca1434ab2d95a4d317cee84ea4a5b8c1bdcf45fcb001951ac0f95dc4c
BLAKE2b-256 checksum
How to use checksums
b8ecbaf08e8b7340d920f166d524b2598ab985f1c1f53f3260f2c7508ab5a798
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / pytest_autofixture-0.3.0-py3-none-any.whl

Download URL pytest_autofixture-0.3.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45e2c16a4fbeb4189d90cee1c9fd337badc4addb4bb38f3354afa51d93edb0fe
BLAKE2b-256 checksum
How to use checksums
8b946b3d43ead49b77fa4f3a5133ad0516f762d834494bb3d628deb88468ceb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for pytest-autofixture 0.3.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytest_autofixture-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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