test_fixtures_order_autouse_multiple_scopes.py


Overview

This file contains a set of Pytest fixtures and test classes designed to illustrate and verify the order of execution of fixtures with different scopes and the use of `autouse=True` in combination with multiple scopes.

The core focus is on how fixtures declared with `scope="class"`, some with `autouse=True`, affect the order in which they are invoked and how their side effects (here, appending to a shared list) manifest in test classes that request or do not request certain fixtures explicitly.


Detailed Explanation

Fixtures

order

@pytest.fixture(scope="class")
def order():
    return []

c1

@pytest.fixture(scope="class", autouse=True)
def c1(order):
    order.append("c1")

c2

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

c3

@pytest.fixture(scope="class")
def c3(order, c1):
    order.append("c3")

Test Classes

TestClassWithC1Request

class TestClassWithC1Request:
    def test_order(self, order, c1, c3):
        assert order == ["c1", "c3"]

TestClassWithoutC1Request

class TestClassWithoutC1Request:
    def test_order(self, order, c2):
        assert order == ["c1", "c2"]

Important Implementation Details and Behavior


Interaction with Other System Components

This file is primarily a standalone test module focusing on Pytest fixture behavior and ordering. It does not interact with other parts of the system directly but serves as a documentation and test example to verify and demonstrate how autouse fixtures with multiple scopes behave in Pytest.

It can be used as a reference or baseline example in a larger test suite that uses complex fixture scopes and dependencies.


Usage Example

Run these tests with Pytest:

pytest test_fixtures_order_autouse_multiple_scopes.py

You should see both tests pass, confirming fixture execution order as designed.


Visual Diagram

The following Mermaid class diagram represents the fixtures and test classes, illustrating dependencies and autouse behavior:

classDiagram
    class order {
        <<fixture>>
        +scope: class
        +returns list
    }
    class c1 {
        <<fixture>>
        +scope: class
        +autouse: True
        +depends_on: order
        +appends "c1"
    }
    class c2 {
        <<fixture>>
        +scope: class
        +depends_on: order
        +appends "c2"
    }
    class c3 {
        <<fixture>>
        +scope: class
        +depends_on: order, c1
        +appends "c3"
    }
    class TestClassWithC1Request {
        +test_order(order, c1, c3)
    }
    class TestClassWithoutC1Request {
        +test_order(order, c2)
    }

    c1 --> order
    c2 --> order
    c3 --> order
    c3 --> c1

    TestClassWithC1Request --> order
    TestClassWithC1Request --> c1
    TestClassWithC1Request --> c3

    TestClassWithoutC1Request --> order
    TestClassWithoutC1Request --> c2
    %% Autouse c1 is implicitly linked to TestClassWithoutC1Request
    TestClassWithoutC1Request ..> c1 : autouse

Summary

This file is an illustrative Pytest test module that explores how `autouse` fixtures with class scope interact with explicitly requested fixtures, and how fixture dependencies influence execution order. The shared mutable `order` list provides a simple mechanism to verify and assert the fixture execution sequence in different test scenarios.