test_cacheprovider.py


Overview

`test_cacheprovider.py` is a comprehensive test suite for verifying the caching functionality provided by pytest's cache provider system. This file contains multiple test classes and standalone test functions that ensure the correctness, robustness, and expected behavior of the cache API integrated within pytest's configuration and runtime environment.

The tests cover various scenarios including cache directory creation, permission handling, data serialization and retrieval, failure cases, integration with pytest's last-failed and new-first test ordering features, and the presence of auxiliary files like [.gitignore](/projects/286/67223) and [CACHEDIR.TAG](/projects/286/67341) in the cache directory.

The file relies heavily on pytest's testing utilities such as [Pytester](/projects/286/67470) for creating test environments and running pytest subprocesses, and [MonkeyPatch](/projects/286/67332) for environment manipulation. It interacts primarily with the [_pytest.cacheprovider.Cache](/projects/286/67341) class and pytest's configuration cache interface.


Classes and Their Responsibilities

1. TestNewAPI

Tests for the new cache API features and behaviors, including:

**Key Methods:**


2. TestLastFailed

Tests related to the [--last-failed](/projects/286/67385) ([--lf](/projects/286/67298)) and [--failed-first](/projects/286/67385) ([--ff](/projects/286/67298)) options of pytest, which interact closely with the cache to rerun failed tests preferentially.

**Key Features Tested:**

**Key Methods:**


3. TestNewFirst

Tests for the [--new-first](/projects/286/67223) ([--nf](/projects/286/67298)) option which orders tests so that newer tests (or those with newer modification times) are run first.

**Key Methods:**


4. TestReadme

Tests that the README file is created inside the cache directory regardless of test outcomes (pass/fail).

**Key Methods:**


5. Action (Enum)

An enumeration to represent cache directory actions used in parameterized tests.


Standalone Test Functions


Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

Using Cache in Tests

def test_cachefuncarg(cache):
    val = cache.get("some/thing", None)
    assert val is None
    cache.set("some/thing", [1])
    pytest.raises(TypeError, lambda: cache.get("some/thing"))  # usage without default fails
    val = cache.get("some/thing", [])
    assert val == [1]

Creating Cache Directories

p = config.cache.mkdir("name")
assert p.is_dir()

Customizing Cache Directory

In `pytest.ini`:

[pytest]
cache_dir = custom_cache_dir/subdir

Mermaid Class Diagram

classDiagram
    class TestNewAPI {
        +test_config_cache_mkdir(pytester)
        +test_cache_dir_permissions(pytester)
        +test_config_cache_dataerror(pytester)
        +test_cache_writefail_cachefile_silent(pytester)
        +test_cache_writefail_permissions(unwritable_cache_dir, pytester)
        +test_cache_failure_warns(pytester, monkeypatch, unwritable_cache_dir)
        +test_config_cache(pytester)
        +test_cachefuncarg(pytester)
        +test_custom_rel_cache_dir(pytester)
        +test_custom_abs_cache_dir(pytester, tmp_path_factory)
        +test_custom_cache_dir_with_env_var(pytester, monkeypatch)
        +unwritable_cache_dir(pytester)
    }

    class TestLastFailed {
        +test_lastfailed_usecase(pytester, monkeypatch)
        +test_failedfirst_order(pytester)
        +test_lastfailed_failedfirst_order(pytester)
        +test_lastfailed_difference_invocations(pytester, monkeypatch)
        +test_lastfailed_usecase_splice(pytester, monkeypatch)
        +test_lastfailed_xpass(pytester)
        +test_xfail_not_considered_failure(pytester)
        +test_xfail_strict_considered_failure(pytester)
        +test_failed_changed_to_xfail_or_skip(pytester, mark)
        +test_lf_and_ff_prints_no_needless_message(quiet, opt, pytester)
        +get_cached_last_failed(pytester)
        +test_cache_cumulative(pytester)
        +test_lastfailed_no_failures_behavior_all_passed(pytester)
        +test_lastfailed_no_failures_behavior_empty_cache(pytester)
        +test_lastfailed_skip_collection(pytester)
        +test_lastfailed_skip_collection_with_nesting(pytester)
        +test_lastfailed_with_known_failures_not_being_selected(pytester)
        +test_lastfailed_args_with_deselected(pytester)
        +test_lastfailed_with_class_items(pytester)
        +test_lastfailed_with_all_filtered(pytester)
        +test_packages(pytester)
        +test_non_python_file_skipped(pytester, dummy_yaml_custom_test)
        +test_lastfailed_collectfailure(pytester, monkeypatch)
        +test_lastfailed_failure_subset(pytester, monkeypatch)
        +test_lastfailed_creates_cache_when_needed(pytester)
    }

    class TestNewFirst {
        +test_newfirst_usecase(pytester)
        +test_newfirst_parametrize(pytester)
    }

    class TestReadme {
        +check_readme(pytester)
        +test_readme_passed(pytester)
        +test_readme_failed(pytester)
    }

    class Action {
        <<enumeration>>
        +MKDIR
        +SET
    }

Summary

`test_cacheprovider.py` is a key part of pytest's internal test suite validating the cache provider system. It ensures that the cache API works correctly under a wide range of conditions, integrates properly with pytest features like last-failed and new-first test execution, and manages cache directory contents and permissions properly. The test suite is organized into test classes focusing on related functionalities and uses pytest's own test utilities extensively to verify behavior in realistic scenarios.

This file contributes to pytest's reliability and user experience by preventing regressions and ensuring caching features behave as expected across test runs.