cache.rst

Overview

The [cache.rst](/projects/286/67341) file documents the **pytest cache provider plugin**, which is an integral component of the `pytest` testing framework. Its primary purpose is to **manage test state and results across test runs**, enabling features such as rerunning only failed tests, prioritizing failures, and persisting arbitrary JSON-serializable data between test sessions.

Key functionalities covered in this documentation include:

This file serves both as a user guide for leveraging these features during test runs and as a reference for plugin developers who want to interact with pytest’s caching mechanism.


Detailed Explanation of Features and Usage

Command-line Options for Failure-based Test Execution

The plugin introduces several CLI options that control how tests are selected and ordered based on previous results.

Option

Description

`--lf`, `--last-failed`

Runs only the tests that failed in the last pytest invocation.

`--ff`, `--failed-first`

Runs previously failing tests first, then runs the rest of the test suite.

`--cache-clear`

Clears all cached data before the test run begins.

`--nf`, [--new-first](/projects/286/67223)

Runs new tests first, then runs the rest, with tests sorted by file modification time.

--lfnf

Governs behavior of `--last-failed` when no failures exist (`all` or `none` modes).

`--sw`, `--stepwise`

Runs tests until the first failure, then stops; on subsequent runs continues from last failure.

`--stepwise-skip`

Skips one failing test in stepwise mode, continuing to next failure. Enables stepwise implicitly.

Usage Examples


The config.cache Object

Plugins and test support code can use the `config.cache` object to **store and retrieve persistent JSON-serializable data** across test runs. This enables expensive computations or state that should be reused without rerunning.

API Methods

Example Usage in a Plugin or Fixture

import pytest

def expensive_computation():
    print("running expensive computation...")
    return 42

@pytest.fixture
def mydata(pytestconfig):
    val = pytestconfig.cache.get("example/value", None)
    if val is None:
        val = expensive_computation()
        pytestconfig.cache.set("example/value", val)
    return val

def test_function(mydata):
    assert mydata == 23

Inspecting and Clearing Cache Contents


Stepwise Testing Mode

The `--sw` or `--stepwise` option allows developers to **incrementally fix failing tests one at a time**:

This mode is especially useful when many tests fail, enabling a focused fix-and-verify cycle.


Implementation Details and Algorithms


Interactions with Other System Components


Visual Diagram: Plugin Structure and Workflow

flowchart TD
    A[pytest CLI Invocation] --> B{Parse CLI Options}
    B -->|--lf / --ff / --sw| C[Cache Plugin]
    B -->|Other options| D[pytest core]

    C --> E[Load .pytest_cache Data]
    E --> F{Determine Tests to Run}
    F -->|--lf| G[Select Failed Tests Only]
    F -->|--ff| H[Run Failed Tests First]
    F -->|--sw| I[Run Tests Until Failure]
    F -->|Default| J[Run All Tests]

    G --> K[pytest Collection - Filtered]
    H --> K
    I --> K
    J --> K

    K --> L[pytest Test Execution]
    L --> M[Update Cache with Results]
    M --> E

    subgraph Cache API Usage
    N[Plugins/Fixtures] --> O[config.cache.get/set]
    end

    style C fill:#f9f,stroke:#333,stroke-width:2px

Summary

The [cache.rst](/projects/286/67341) documentation provides a comprehensive guide to pytest’s cache provider plugin. This plugin enhances testing efficiency by enabling reruns of failed tests, caching persistent state, and providing flexible test execution options. It is an essential tool for speeding up test cycles, especially in large test suites, and serves as a foundation for advanced test session management.

By understanding this plugin’s capabilities and APIs, users and plugin authors can leverage caching to improve test reliability, reduce runtime, and create sophisticated test workflows.

For more details, see also the `config.cache` fixture documentation and pytest command-line option references.