test_pytester.py


Overview

The [test_pytester.py](/projects/286/67377) file is a comprehensive test suite designed to verify the functionality, robustness, and integration of the `pytester` testing utility within the `pytest` framework. The file primarily contains unit and integration tests that:

This file interacts extensively with the `_pytest.pytester` module and other pytest internals, serving as a critical validation layer for `pytester` functionality to ensure reliable test execution and plugin support.


Detailed Explanations

Key Classes and Functions

**Note:** The file mostly contains test functions and test classes; the main entities tested come from the `_pytest.pytester` module such as `Pytester`, `HookRecorder`, `SysModulesSnapshot`, and `SysPathsSnapshot`.


test_make_hook_recorder(pytester: Pytester) -> None


test_parseconfig(pytester: Pytester) -> None


test_pytester_runs_with_plugin(pytester: Pytester) -> None


test_pytester_with_doctest(pytester: Pytester) -> None


test_runresult_assertion_on_xfail and test_runresult_assertion_on_xpassed


test_xpassed_with_strict_is_considered_a_failure


make_holder() -> tuple[type, ModuleType]


test_hookrecorder_basic(holder)


test_makepyfile_unicode and test_makepyfile_utf8


TestInlineRunModulesCleanup


TestSysModulesSnapshot


TestSysPathsSnapshot


test_pytester_subprocess and test_pytester_subprocess_via_runpytest_arg


test_unicode_args


test_pytester_run_no_timeout, test_pytester_run_with_timeout, and test_pytester_run_timeout_expires


test_linematcher_* functions


test_pytest_addopts_before_pytester


test_run_stdin, test_popen_stdin_pipe, test_popen_stdin_bytes, test_popen_default_stdin_stderr_and_stdin_None


test_spawn_uses_tmphome


test_run_result_repr


test_pytester_outcomes_with_multiple_errors


test_parse_summary_line_always_plural


test_makefile_joins_absolute_path and test_pytester_makefile_dot_prefixes_extension_with_warning


test_pytester_assert_outcomes_warnings and test_pytester_outcomes_deselected


test_pytester_subprocess_with_string_plugins and test_pytester_subprocess_with_non_string_plugins


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class Pytester {
        +makepyfile(content)
        +runpytest(args)
        +runpytest_subprocess(args)
        +inline_run(args)
        +make_hook_recorder(pluginmanager)
        +popen(args)
        +spawn_pytest(path)
        +syspathinsert()
    }

    class HookRecorder {
        +getfailures()
        +listoutcomes()
        +countoutcomes()
        +getfailedcollections()
        +popcall(name)
        +unregister()
        +clear()
    }

    class SysModulesSnapshot {
        -__snapshot: dict
        +restore()
    }

    class SysPathsSnapshot {
        +restore()
    }

    class LineMatcher {
        +fnmatch_lines(lines, consecutive=False)
        +re_match_lines(lines, consecutive=False)
        +no_fnmatch_line(pattern)
        +no_re_match_line(pattern)
        +_getlines(input)
        +__str__()
    }

    Pytester --> HookRecorder : creates
    Pytester --> SysModulesSnapshot : uses
    Pytester --> SysPathsSnapshot : uses
    Pytester --> LineMatcher : uses for output matching
    HookRecorder --> PytestPluginManager : hooks into

Summary

The [test_pytester.py](/projects/286/67377) file is a thorough test suite validating the `pytester` testing utility's features, including hook recording, subprocess test execution, module and path snapshotting, output matching, and environment isolation. It ensures that `pytester` behaves correctly under various scenarios and integrates properly with pytest’s plugin and test-running infrastructure. The file’s tests cover both normal and edge cases, making it a critical component for maintaining the reliability and functionality of pytest's testing utilities.