Pytest works like this: If you have a fixture and a test which refers to the fixture: @pytest.fixture def get_date(): return time.time() def test_mytime(get_date): assert get_date < time.time() Pytest will automatically call the fixture function and the function’s return value will be av… Answer from gkb on discuss.python.org
🌐
pytest
docs.pytest.org › en › 6.2.x › explanation › fixtures.html
pytest fixtures: explicit, modular, scalable — pytest documentation
For test_req and test_no_req inside TestClassWithAutouse, c3 effectively makes c2 an autouse fixture, which is why c2 and c3 are executed for both tests, despite not being requested, and why c2 and c3 are executed before c1 for test_req. If this made c2 an actual autouse fixture, then c2 would also execute for the tests inside TestClassWithoutAutouse, since they can reference c2 if they wanted to.
🌐
pytest
docs.pytest.org › en › stable › how-to › parametrize.html
How to parametrize fixtures and test functions - pytest documentation
# content of conftest.py def pytest_addoption(parser): parser.addoption( "--stringinput", action="append", default=[], help="list of stringinputs to pass to test functions", ) def pytest_generate_tests(metafunc): if "stringinput" in metafunc.fixturenames: metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput"))
Discussions

Pytest and autouse
Hello, Anyone can you explain me why in pytest fixture using autouse and this fixture return a value doesn’t work? I create a example very basic. @pytest.fixture(autouse=True, scope="session") def get_date(): now = time.time() time.sleep(1) return get_date def test_mytime(): assert get_date More on discuss.python.org
🌐 discuss.python.org
11
0
December 29, 2023
python - pytest autouse=True not working correctly? simply returning ["a"] - Stack Overflow
I thought the fixture will be processed first by append_first so always return [first_entry]? ... I removed my comments and put them in a separate answer instead, hopefully this clarifies it. ... I'll make an answer from my comments, as this may indeed be confusing for more people. The problem here is the example in the pytest ... More on stackoverflow.com
🌐 stackoverflow.com
Pytest get fixture execution order
I do understand that it is too late for you, but probably will be useful for others - there is "--setup-show" option More on reddit.com
🌐 r/learnpython
2
2
October 24, 2022
Does pytest break a lot of coding rules?

I've been using pytest for years. I am a python programmer with 15 years of experience.

I hate pytest design. I hate its magic behavior. I hate its reliance on exactly named arguments. I hate its use of conftest.py. I hate literally everything about it.

But all this hatred is from the formal point of view. The truth is that it's damn fast to write tests in it once you submit to its logic. Plus, there's a huge amount of plugins that you would have to reinvent if you were to use anything else.

So,... just accept it.

More on reddit.com
🌐 r/Python
45
52
June 7, 2022
🌐
Playwright
playwright.dev › fixtures
Fixtures | Playwright
The { page } argument tells Playwright Test to set up the page fixture and provide it to your test function.
🌐
Codeinthehole
til.codeinthehole.com › posts › you-can-have-classscoped-autouse-pytest-fixtures
TIL You can have class-scoped autouse pytest fixtures — David Winterbottom
March 16, 2021 - If you decorate a class instance method as an autouse=True Pytest fixture, then it will be automatically applied for all test methods on that class. This is a useful pattern to remember.
🌐
Python.org
discuss.python.org › python help
Pytest and autouse - Python Help - Discussions on Python.org
December 29, 2023 - Hello, Anyone can you explain me why in pytest fixture using autouse and this fixture return a value doesn’t work? I create a example very basic. @pytest.fixture(autouse=True, scope="session") def get_date(): now =…
🌐
pytest
docs.pytest.org › en › stable › how-to › fixtures.html
How to use fixtures - pytest documentation
We can make a fixture an autouse fixture by passing in autouse=True to the fixture’s decorator.
Find elsewhere
🌐
Leyaa
leyaa.ai › codefly › learn › pytest › part-1 › pytest-autouse-fixtures › why
Why Autouse fixtures in PyTest - When & Why You Need It | Leyaa.ai
Autouse fixtures automatically run setup code before each test without needing to call them explicitly. This means your tests stay clean, and the setup always happens, like magic behind the scenes.
🌐
DevGenius
blog.devgenius.io › pytest-fixtures-your-secret-weapon-for-writing-powerful-tests-5d854a01d4a6
Pytest Fixtures: Your Secret Weapon for Writing Powerful Tests | by Tomas Svojanovsky | Dev Genius
April 24, 2024 - We can also specify the test scope; even if the fixture is called for each test, we can change to another scope like module. They also allow easy use for setup and teardown logic. I think they are a must-have tool for every Python developer who wants to use pytest.
🌐
pytest
docs.pytest.org › en › stable › reference › fixtures.html
Fixtures reference - pytest documentation
For test_req and test_no_req inside TestClassWithAutouse, c3 effectively makes c2 an autouse fixture, which is why c2 and c3 are executed for both tests, despite not being requested, and why c2 and c3 are executed before c1 for test_req. If this made c2 an actual autouse fixture, then c2 would also execute for the tests inside TestClassWithoutAutouse, since they can reference c2 if they wanted to.
🌐
Rednafi
rednafi.com › python › inject-pytest-fixture
Injecting Pytest fixtures without cluttering test signatures | Redowan's Reflections
December 2, 2024 - One way to avoid this is by marking the mock_env fixture with @pytest.fixture(autouse=True) and omitting it from the test function’s parameters.
Top answer
1 of 1
4

I'll make an answer from my comments, as this may indeed be confusing for more people.

The problem here is the example in the pytest documentation mentioned in the question, that was used as a template:

@pytest.fixture
def first_entry():
    return "a"

@pytest.fixture
def order(first_entry):
    return []

@pytest.fixture(autouse=True)
def append_first(order, first_entry):
    return order.append(first_entry)

def test_string_only(order, first_entry):
    assert order == [first_entry]

This is not a good example to base your code on. What happens here is that the fixture order returns an empty array, and the fixture append_first adds an entry to this array. Due to the fact that append_first is always called, it always appends that entry to the array instance returned from the first fixture. The fact that it also returns it is irrelevant for this case - it would only be needed if the fixture was called directly.

So if using instead:

@pytest.fixture(autouse=True)
def append_first(order, first_entry):
    order.append(first_entry)
    # does not return anything

would have the same effect, namely the side effect of altering the array object returned by the first fixture.

This is unexpected and confusing, and very similar in my opinion to the known anti-pattern of passing an array as a default argument.

As mentioned, fixtures with autouse=True make only sense if they have a side effect, and they usually don't return anything. They may return a value, but that value will only be used if the fixture is used directly, otherwise it will be discarded.

In your example, you simply don't need an autouse fixture, but can just reference the fixture directly, as already mentioned in the other answer:

@pytest.fixture
def first_entry():
    return "a"


@pytest.fixture
def append_first(first_entry):
    return [first_entry]


def test_string_only(append_first):
    assert append_first == ["a"]

Note also that the mentioned pytest example has been changed in the current documentation to not return a value, and received a better explanation.

🌐
PythonTest
pythontest.com › framework › pytest › pytest-mark-usefixtures
Using pytest fixtures with mark.usefixtures | PythonTest
July 12, 2025 - import pytest pytestmark = pytest.mark.usefixtures("before") @pytest.fixture() def before(): print('\nbefore each test') def test_1(): ... def test_2(): ... If you’re defining a fixture in the same test file, this is less common, as you can just use autouse.
🌐
DataCamp
campus.datacamp.com › courses › introduction-to-testing-in-python › pytest-fixtures
Fixtures autouse | Python
All such cases should be addressed with an autouse argument. Look at this code. Here we have two functions: a fixture "set_pd_options" that is autoused and a test function "test_pd_options". In this example, we are changing the parameter "display max columns" of the "pandas" library from its default value (which is 20, by the way) to five thousand.
🌐
Readthedocs
happytest.readthedocs.io › en › latest › fixture
pytest fixtures: explicit, modular, scalable — pytest documentation
The class-level transact fixture is marked with autouse=true which implies that all test methods in the class will use this fixture without a need to state it in the test function signature or with a class-level usefixtures decorator.
🌐
Aptuz
aptuz.com › blog › the-power-of-pytest-fixtures-setup-once-test-everywhere
Pytest Fixtures: Setup Once, Test Everywhere
September 3, 2025 - At its core, a Pytest fixture is a reusable function that sets up the context or data your tests need.
🌐
PythonTest
pythontest.com › testandcode › episodes › pytest-autouse-fixtures
Test and Code 205 - pytest autouse fixtures | PythonTest
July 31, 2023 - So, fixtures and pytest are a way of basically performing setup and and tear down actions, And it’s done in a way that’s, more considered dependency injection where, you know, you you define it somewhere and then just list it as a parameter in your test function, and, pytest is smart enough to go find it and go grab it and use it and kind of insert it into your test.