issue_519.py


Overview

This file contains pytest test fixtures and tests designed to demonstrate and verify the order and scoping behavior of parameterized fixtures in pytest. It specifically tests how fixtures with different scopes (`session`, `module`, and `function`) interact when parameterized and combined in tests. The tests and fixtures track the invocation order and parameter values used, asserting that pytest’s parameterized fixture expansion and scoping behave as expected.

The core functionality centers around:


Detailed Explanation of Components

Functions

pytest_generate_tests(metafunc)


Fixtures

checked_order


fix1(request, arg1, checked_order)


fix2(request, fix1, arg2, checked_order)


Test Functions

test_one(fix2)


test_two(fix2)


Implementation Details and Algorithms


Interaction with Other System Components


Usage Example

From the command line, run tests with:

pytest issue_519.py -v

This will execute `test_one` and `test_two` with all combinations of `arg1` and `arg2`. After tests complete, the `checked_order` fixture prints the recorded execution order and asserts the expected order.


Visual Diagram

classDiagram
    class pytest_generate_tests {
        +metafunc
        +parametrize(arg1 or arg2)
    }

    class checked_order {
        +list[tuple(str,str,str)] order
        +yield order
        +assert order
    }

    class fix1 {
        +request
        +arg1
        +checked_order
        +yield "fix1-" + arg1
    }

    class fix2 {
        +request
        +fix1
        +arg2
        +checked_order
        +yield "fix2-" + arg2 + fix1
    }

    class test_one {
        +fix2
    }

    class test_two {
        +fix2
    }

    pytest_generate_tests --> fix1 : parametrize "arg1"
    pytest_generate_tests --> fix2 : parametrize "arg2"
    fix1 --> checked_order : append (node, fix1, arg1)
    fix2 --> checked_order : append (node, fix2, arg2)
    fix2 --> fix1 : depends on
    test_one --> fix2 : uses
    test_two --> fix2 : uses
    checked_order : shared session fixture

Summary

This file demonstrates advanced pytest fixture usage with parameterization and scoping. Through careful tracking of fixture invocation order and parameters, it validates pytest’s execution model when combining module- and function-scoped fixtures with parameter sets. It serves as a useful reference or test case for pytest behavior verification.