pytest-cache
pytest-cache was an external plugin for pytest that provided mechanisms for caching data across test runs, including options to re-run only last failed tests (`--lf`) or run failed tests first (`--ff`), and a `config.cache` object for plugins to store and retrieve values. Its core functionality was integrated into pytest itself starting around versions 2.8 and 3.8. The last release of the standalone `pytest-cache` plugin was version 1.0 in June 2013, making it effectively abandoned as its features are now part of `pytest` core.
Warnings
- breaking The standalone `pytest-cache` plugin is obsolete. Its core functionalities, such as the `cache` fixture and command-line options like `--lf` (last failed) and `--ff` (failed first), were integrated directly into `pytest` starting with `pytest` 2.8 and later refined. Directly installing and using `pytest-cache` with modern `pytest` versions is not recommended and may lead to unexpected behavior or conflicts.
- deprecated The `pytest-cache` plugin itself has not been updated since June 2013 (version 1.0) and is considered abandoned. All its relevant features are now maintained and provided by the `pytest` core. Relying on this old, external plugin is unnecessary and potentially insecure.
- gotcha The cache directory for `pytest` was renamed from `.cache` to `.pytest_cache` to clarify its purpose. If migrating from very old `pytest-cache` setups, be aware of this change. The `.pytest_cache` directory is typically created in the project's root directory and should generally be ignored by version control systems, although `pytest` now often adds a `.gitignore` file within it automatically.
Install
-
pip install pytest-cache
Quickstart
# The functionality of pytest-cache is now built into pytest.
# To access the cache in modern pytest:
def test_example_with_cache(cache):
# Get a value from the cache, with a default if not found
cached_value = cache.get('myplugin/some_key', 'default_value')
print(f"Cached value: {cached_value}")
# Set a value in the cache
cache.set('myplugin/some_key', 'new_value')
# To run only last failed tests:
# pytest --lf
# To run failed tests first, then the rest:
# pytest --ff
# To clear the cache:
# pytest --cache-clear