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 []
Purpose:
Provides a fresh empty list for each test class scope. This list is used to track the order in which other fixtures are executed.Scope:
"class"
The fixture is created once per test class.Parameters: None
Returns:
A new empty list.Usage:
Injected into test methods and other fixtures to record execution order.
c1
@pytest.fixture(scope="class", autouse=True)
def c1(order):
order.append("c1")
Purpose:
Automatically appends"c1"to the sharedorderlist before any tests in a test class are executed.Scope:
"class"Autouse:
True
This fixture is automatically used for each test class without explicit request.Parameters:
order: the shared list fixture.
Returns: None
Side Effect:
Modifiesorderby appending"c1".Usage:
Implicitly invoked for any test class to ensure"c1"is recorded at the start.
c2
@pytest.fixture(scope="class")
def c2(order):
order.append("c2")
Purpose:
Appends"c2"to the sharedorderlist when explicitly requested.Scope:
"class"Autouse: Not used (default
False).Parameters:
order: the shared list fixture.
Returns: None
Side Effect:
Modifiesorderby appending"c2".Usage:
Used only when requested explicitly by a test method.
c3
@pytest.fixture(scope="class")
def c3(order, c1):
order.append("c3")
Purpose:
Appends"c3"to the sharedorderlist. It depends onc1, soc1must be executed beforec3.Scope:
"class"Autouse: Not used.
Parameters:
order: the shared list fixture.c1: the autouse fixture, ensuring it runs beforec3.
Returns: None
Side Effect:
Appends"c3"toorder.Usage:
Can be explicitly requested, and guarantees that"c1"is appended before"c3"due to fixture dependency.
Test Classes
TestClassWithC1Request
class TestClassWithC1Request:
def test_order(self, order, c1, c3):
assert order == ["c1", "c3"]
Purpose:
Tests the order of fixture execution whenc1andc3are explicitly requested.Test Method:
test_orderParameters:
order(list): shared order list.c1(fixture): autouse fixture, here also explicitly requested.c3(fixture): fixture that depends onc1.
Behavior:
Asserts that theorderlist contains["c1", "c3"].
Sincec1is autouse and explicitly requested, it runs first, thenc3appends after.
Expected Outcome:
The test passes, verifying the correct order of fixture execution.
TestClassWithoutC1Request
class TestClassWithoutC1Request:
def test_order(self, order, c2):
assert order == ["c1", "c2"]
Purpose:
Tests the order of fixture execution when onlyc2is explicitly requested, butc1is autouse and not requested explicitly.Test Method:
test_orderParameters:
order(list): shared order list.c2(fixture): explicitly requested fixture.
Behavior:
Sincec1isautouse=True, it runs automatically before any tests in this class, appending"c1". Then,c2appends"c2".Assertion:
Theorderlist should be["c1", "c2"].
Expected Outcome:
The test passes, confirming that autouse fixtures with class scope run even when not explicitly requested.
Important Implementation Details and Behavior
Fixture Scope:
All fixtures are declared with
scope="class", meaning they are set up once per test class, preserving theorderlist for all tests in that class.Autouse Fixture:
The
c1fixture hasautouse=True, making it automatically active for every test class, regardless of explicit request. This ensures"c1"is always appended first before any other fixtures.Fixture Dependencies:
The fixture
c3depends onc1. This dependency ensures thatc1is executed beforec3, even ifc1were not autouse. Here, it reinforces the execution order.Order Tracking via Mutable Object:
The shared
orderlist is used to record the sequence of fixture execution steps. Since all fixtures append to the same list object, the final contents reflect the order of execution.Test Coverage:
The two test classes demonstrate two scenarios:
One where
c1andc3are explicitly requested.One where only
c2is requested, butc1still runs due to autouse.
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.