test_fixtures_order_autouse_temp_effects.py


Overview

This test module explores the behavior and ordering effects of **pytest fixtures**, particularly focusing on fixtures with `autouse=True` and their interaction with explicitly requested fixtures within test classes.

The main purpose is to demonstrate how autouse fixtures (fixtures that are automatically applied to tests without explicit request) affect the order of fixture execution and how this impacts the test results when combined with other fixtures.

More specifically, it:

This file is useful for pytest users who want to understand or verify the side-effects and execution order of autouse fixtures relative to explicitly requested fixtures.


Detailed Explanation of Components

Fixtures

Pytest fixtures are functions decorated with `@pytest.fixture` used to set up test state or dependencies.

order()

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

c1(order)

@pytest.fixture
def c1(order):
    order.append("c1")

c2(order)

@pytest.fixture
def c2(order):
    order.append("c2")

Classes and Their Methods

TestClassWithAutouse

class TestClassWithAutouse:
    @pytest.fixture(autouse=True)
    def c3(self, order, c2):
        order.append("c3")

    def test_req(self, order, c1):
        assert order == ["c2", "c3", "c1"]

    def test_no_req(self, order):
        assert order == ["c2", "c3"]

**Key behaviors:**


TestClassWithoutAutouse

class TestClassWithoutAutouse:
    def test_req(self, order, c1):
        assert order == ["c1"]

    def test_no_req(self, order):
        assert order == []

Important Implementation Details and Behavior


Interactions with Other Parts of the System


Usage Examples

To run these tests, simply execute pytest on this module:

pytest test_fixtures_order_autouse_temp_effects.py

Expected output: all tests pass, confirming fixture invocation order.


Mermaid Class Diagram

classDiagram
    class TestClassWithAutouse {
        +c3(order, c2)
        +test_req(order, c1)
        +test_no_req(order)
    }
    class TestClassWithoutAutouse {
        +test_req(order, c1)
        +test_no_req(order)
    }
    class Fixtures {
        +order() : list
        +c1(order)
        +c2(order)
    }
    TestClassWithAutouse ..> Fixtures : uses
    TestClassWithoutAutouse ..> Fixtures : uses
    Fixtures : c3 depends on c2

Summary

This file provides a focused, executable demonstration of pytest fixture ordering with autouse fixtures and dependencies. It is valuable for pytest users wanting to understand fixture execution side-effects and the interplay between autouse and explicitly requested fixtures.