test_session.py


Overview

The [test_session.py](/projects/286/67371) file contains a comprehensive suite of tests designed to validate the behavior and robustness of pytest's session and collection mechanisms. It primarily leverages pytest's own testing utilities (`Pytester`, `MonkeyPatch`) to simulate various test scenarios and validate pytest's session lifecycle events, error handling, test collection, test execution order, plugin management, and command line options related to test session execution.

This file serves as a meta-test suite for pytest itself, ensuring that the pytest test session lifecycle and related features work correctly under diverse conditions, including syntax errors, import errors, plugin conflicts, and command line flags like [--exitfirst](/projects/286/67385), `--maxfail`, [--ignore](/projects/286/67223), and [--deselect](/projects/286/67223).


Classes and their Methods

Class: SessionTests

This class groups tests that verify fundamental pytest session behaviors, including test collection, event triggering, error reporting, and handling of edge cases like broken [__repr__](/projects/286/67223) implementations.

Methods:


Class: TestNewSession(SessionTests)

This class extends `SessionTests` and adds tests focused on more complex session scenarios, such as execution order, collection-only mode, and interaction of multiple command-line options.

Methods:


Standalone Test Functions

Several standalone test functions at module level verify pytest's plugin handling, command line options, and session attributes such as [shouldfail](/projects/286/67385) and [shouldstop](/projects/286/67210).


Implementation Details and Algorithms


Interaction with Other System Components


Usage Examples

Example: Testing basic test item events

def test_basic_testitem_events(pytester: Pytester) -> None:
    tfile = pytester.makepyfile(
        '''
        def test_one():
            pass
        def test_fail():
            assert False
        '''
    )
    reprec = pytester.inline_run(tfile)
    passed, skipped, failed = reprec.listoutcomes()
    assert len(passed) == 1
    assert len(failed) == 1

Example: Using --maxfail option in tests

def test_maxfail(pytester: Pytester) -> None:
    reprec = pytester.inline_runsource(
        '''
        def test_one(): assert 0
        def test_two(): assert 0
        def test_three(): assert 0
        ''',
        '--maxfail=2',
    )
    passed, skipped, failed = reprec.countoutcomes()
    assert failed == 2

Mermaid Class Diagram

classDiagram
    class SessionTests {
        +test_basic_testitem_events(pytester: Pytester) void
        +test_nested_import_error(pytester: Pytester) void
        +test_raises_output(pytester: Pytester) void
        +test_syntax_error_module(pytester: Pytester) void
        +test_exit_first_problem(pytester: Pytester) void
        +test_maxfail(pytester: Pytester) void
        +test_broken_repr(pytester: Pytester) void
        +test_broken_repr_with_showlocals_verbose(pytester: Pytester) void
        +test_skip_file_by_conftest(pytester: Pytester) void
    }

    class TestNewSession {
        +test_order_of_execution(pytester: Pytester) void
        +test_collect_only_with_various_situations(pytester: Pytester) void
        +test_minus_x_import_error(pytester: Pytester) void
        +test_minus_x_overridden_by_maxfail(pytester: Pytester) void
    }

    SessionTests <|-- TestNewSession

Summary

[test_session.py](/projects/286/67371) is a critical part of pytest's own test infrastructure, ensuring that pytest's session management, test collection, execution order, error handling, and configuration options work correctly. It uses `Pytester` to dynamically create test scenarios and validate pytest's internal behaviors. The file is comprehensive and covers a wide variety of edge cases, contributing to pytest's robustness and reliability.