test_fixtures_order_scope.py
Overview
This file defines a set of Pytest fixtures with different scopes to demonstrate and verify the order in which fixtures of varying scopes are executed within a test session. It is primarily used to test and validate the execution order of fixtures scoped at **function**, **class**, **module**, **package**, and **session** levels in Pytest.
The key functionality is to append identifying strings ("function", "class", "module", "package", "session") to a shared list (`order`) in accordance with the fixture scope execution order, and then assert that the order matches the expected sequence. This helps developers understand and confirm Pytest's fixture scope precedence and lifecycle during testing.
Detailed Explanation of Classes, Functions, and Fixtures
Fixtures
The file defines six fixtures, each with a different scope:
order
Scope:
sessionType: List
Purpose: Shared list that collects the order in which fixtures are executed.
Returns: An empty list which is then passed and mutated by other fixtures to track execution order.
@pytest.fixture(scope="session") def order(): return []func
Scope:
function(default scope)Parameters:
order(the session-scoped list fixture)
Functionality: Appends the string
"function"toorderlist whenever this fixture is used in a test function.Usage: Used to represent function-level fixture execution.
@pytest.fixture def func(order): order.append("function")cls
Scope:
classParameters:
order
Functionality: Appends
"class"toorder. Invoked once per test class scope.Usage: Represents class-level fixture execution.
@pytest.fixture(scope="class") def cls(order): order.append("class")mod
Scope:
moduleParameters:
order
Functionality: Appends
"module"toorder. Invoked once per test module.Usage: Represents module-level fixture execution.
@pytest.fixture(scope="module") def mod(order): order.append("module")pack
Scope:
packageParameters:
order
Functionality: Appends
"package"toorder. Invoked once per package.Usage: Represents package-level fixture execution.
@pytest.fixture(scope="package") def pack(order): order.append("package")sess
Scope:
sessionParameters:
order
Functionality: Appends
"session"toorder. Invoked once per test session.Usage: Represents session-level fixture execution.
@pytest.fixture(scope="session") def sess(order): order.append("session")
Class: TestClass
This class contains a single test method that uses all the fixtures described above to verify their execution order.
Method: test_order
Parameters:
func(function-scoped fixture)cls(class-scoped fixture)mod(module-scoped fixture)pack(package-scoped fixture)sess(session-scoped fixture)order(session-scoped list collecting execution order)
Functionality:
The method asserts that the
orderlist contains the fixture names appended in the expected order:["session", "package", "module", "class", "function"]This assertion confirms the order in which Pytest initializes and runs fixtures based on their scope hierarchy.
Usage Example:
def test_order(self, func, cls, mod, pack, sess, order):
assert order == ["session", "package", "module", "class", "function"]
Important Implementation Details and Algorithms
Fixture Scopes and Execution Order:
Pytest executes fixtures according to their defined scopes in ascending order of their lifetime:session→package→module→class→function. This file explicitly demonstrates and verifies this behavior by appending strings to a shared list in each fixture and asserting the final order.Shared State via Fixture Dependency:
Theorderfixture is declared withsessionscope and returns a mutable list. All other fixtures receive this list as a parameter and append their respective scope identifier. This shared state allows for tracking of the order in which fixtures are executed.No Return Values from Fixtures Except
order:
Fixtures likefunc,cls,mod,pack, andsessdo not return any value; their sole purpose is to mutate theorderlist to track execution.Test Validation:
Thetest_ordermethod indirectly tests fixture ordering by inspecting the accumulatedorderlist, serving as a practical example of fixture scope behavior in Pytest.
Interaction with Other Parts of the System or Application
This file is primarily a test utility and demonstration component within a Pytest-based testing suite.
It interacts with Pytest’s fixture management system, relying on it to handle fixture instantiation and teardown according to scope.
It may be part of a larger test suite where understanding fixture execution order is important for designing reliable tests.
It does not interact directly with application business logic but provides insights to developers on how fixtures behave across different scopes, which can affect test setup and performance.
Visual Diagram
classDiagram
class order {
<<fixture>>
+scope: session
+returns List[str]
}
class func {
<<fixture>>
+scope: function (default)
+params: order
+appends "function" to order
}
class cls {
<<fixture>>
+scope: class
+params: order
+appends "class" to order
}
class mod {
<<fixture>>
+scope: module
+params: order
+appends "module" to order
}
class pack {
<<fixture>>
+scope: package
+params: order
+appends "package" to order
}
class sess {
<<fixture>>
+scope: session
+params: order
+appends "session" to order
}
class TestClass {
+test_order(func, cls, mod, pack, sess, order)
}
TestClass --> func
TestClass --> cls
TestClass --> mod
TestClass --> pack
TestClass --> sess
func --> order
cls --> order
mod --> order
pack --> order
sess --> order
Summary
This file demonstrates Pytest fixture scope ordering by appending identifiers to a shared list.
Fixtures cover all major scopes:
session,package,module,class, andfunction.The test asserts that the execution order matches the expected sequence of fixture initialization.
Useful for understanding Pytest lifecycle behavior and ensuring correct fixture usage in complex test suites.