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:

  1. order

    • Scope: session

    • Type: 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 []
    
  2. func

    • Scope: function (default scope)

    • Parameters:

      • order (the session-scoped list fixture)

    • Functionality: Appends the string "function" to order list 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")
    
  3. cls

    • Scope: class

    • Parameters:

      • order

    • Functionality: Appends "class" to order. Invoked once per test class scope.

    • Usage: Represents class-level fixture execution.

    @pytest.fixture(scope="class")
    def cls(order):
        order.append("class")
    
  4. mod

    • Scope: module

    • Parameters:

      • order

    • Functionality: Appends "module" to order. Invoked once per test module.

    • Usage: Represents module-level fixture execution.

    @pytest.fixture(scope="module")
    def mod(order):
        order.append("module")
    
  5. pack

    • Scope: package

    • Parameters:

      • order

    • Functionality: Appends "package" to order. Invoked once per package.

    • Usage: Represents package-level fixture execution.

    @pytest.fixture(scope="package")
    def pack(order):
        order.append("package")
    
  6. sess

    • Scope: session

    • Parameters:

      • order

    • Functionality: Appends "session" to order. 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

def test_order(self, func, cls, mod, pack, sess, order):
    assert order == ["session", "package", "module", "class", "function"]

Important Implementation Details and Algorithms


Interaction with Other Parts of the System or Application


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