test_stepwise.py

Overview

The [test_stepwise.py](/projects/286/67334) file contains a comprehensive suite of automated tests for the **stepwise testing feature** in the pytest testing framework. The "stepwise" feature is designed to improve debugging efficiency by stopping the test run on the first failure and continuing from that point in subsequent runs, avoiding re-running previously passed tests unnecessarily.

This file verifies the correctness, robustness, and user experience of the stepwise feature, including interactions with pytest command-line options, cache management, error handling, and compatibility with other pytest features such as `xfail` and parallel test execution (`xdist`).


Detailed Explanation of Contents

The file is structured using `pytest` fixtures and test functions, with some internal utility functions. It primarily uses the `Pytester` testing helper from pytest to create temporary test environments and run pytest subprocesses.

Fixtures

Fixtures prepare reusable test environments for different scenarios.

stepwise_pytester(pytester: Pytester) -> Pytester

error_pytester(pytester: Pytester) -> Pytester

broken_pytester(pytester: Pytester) -> Pytester


Utility Functions

_strip_resource_warnings(lines: Sequence[str]) -> Sequence[str]


Test Functions

Each test function uses the `Pytester` fixture to run pytest subprocesses with different options and asserts expected behavior.


test_run_without_stepwise(stepwise_pytester: Pytester)


test_stepwise_output_summary(pytester: Pytester)


test_fail_and_continue_with_stepwise(stepwise_pytester: Pytester)


test_run_with_skip_option(stepwise_pytester: Pytester, stepwise_skip: str)


test_fail_on_errors(error_pytester: Pytester)


test_change_testfile(stepwise_pytester: Pytester)


test_stop_on_collection_errors(broken_pytester: Pytester, broken_first: bool)


test_xfail_handling(pytester: Pytester, monkeypatch: MonkeyPatch)


test_stepwise_skip_is_independent(pytester: Pytester)


test_sw_skip_help(pytester: Pytester)


test_stepwise_xdist_dont_store_lastfailed(pytester: Pytester)


test_disabled_stepwise_xdist_dont_clear_cache(pytester: Pytester)


test_do_not_reset_cache_if_disabled(pytester: Pytester)


test_reset(pytester: Pytester)


test_change_test_count(pytester: Pytester)


test_cache_error(pytester: Pytester)


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

Below are simplified usage scenarios demonstrated in the tests:

# Run tests normally without stepwise
pytest -v --fail

# Run tests with stepwise, stopping at first failure
pytest -v --stepwise --fail

# Fix failing test, rerun with stepwise to continue from failure
pytest -v --stepwise

# Skip previously passed tests but continue after failures
pytest -v --stepwise --stepwise-skip --fail --fail-last

# Reset stepwise state to rerun all tests
pytest -v --stepwise-reset

Mermaid Diagram

classDiagram
    class StepwiseTestSuite {
        +stepwise_pytester(pytester: Pytester) Pytester
        +error_pytester(pytester: Pytester) Pytester
        +broken_pytester(pytester: Pytester) Pytester
        +_strip_resource_warnings(lines: Sequence[str]) Sequence[str]
        +test_run_without_stepwise(stepwise_pytester: Pytester)
        +test_stepwise_output_summary(pytester: Pytester)
        +test_fail_and_continue_with_stepwise(stepwise_pytester: Pytester)
        +test_run_with_skip_option(stepwise_pytester: Pytester, stepwise_skip: str)
        +test_fail_on_errors(error_pytester: Pytester)
        +test_change_testfile(stepwise_pytester: Pytester)
        +test_stop_on_collection_errors(broken_pytester: Pytester, broken_first: bool)
        +test_xfail_handling(pytester: Pytester, monkeypatch: MonkeyPatch)
        +test_stepwise_skip_is_independent(pytester: Pytester)
        +test_sw_skip_help(pytester: Pytester)
        +test_stepwise_xdist_dont_store_lastfailed(pytester: Pytester)
        +test_disabled_stepwise_xdist_dont_clear_cache(pytester: Pytester)
        +test_do_not_reset_cache_if_disabled(pytester: Pytester)
        +test_reset(pytester: Pytester)
        +test_change_test_count(pytester: Pytester)
        +test_cache_error(pytester: Pytester)
    }

Summary

[test_stepwise.py](/projects/286/67334) is a critical quality assurance file for validating the stepwise testing feature in pytest. It ensures that:

This thorough test coverage helps maintain a smooth and predictable developer experience when using pytest's stepwise test execution.