17

pytest-cache

It seems to be a tool to cache func/args->result pairs and even persist them between testsuite-runs. Which seems like a great idea to speed things up. However I haven't noticed any mention of automatically detecting a change of a function source code and invalidating corresponding cache entries. That seems to defeat the purpose of running the test suite, because the tested code changes are not going to be reflected.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
user257754
  • 318
  • 1
  • 2
  • 6

3 Answers3

16

pytest-cache does two things:

  • offer a mechanism through which other plugins can get/set values via config.cache.get|set. This is used by pytest-pep8 and pytest-flakes for example to store the mtime of the last check to avoid re-checking files all the time.

  • store test failures so that you can run --lf to run only the last failures and --ff to run the last failures first, then the rest of the tests.

The functionality is bound to move to the core with pytest-2.7 (not released as of today) or a subsequent release.

hpk42
  • 21,501
  • 4
  • 47
  • 53
  • 1
    As noted below, this is now a standard `pytest` plugin named `cacheprovider`. https://docs.pytest.org/en/stable/cache.html – nealmcb Sep 03 '20 at 02:28
7

In pytest 3.4.0, one of the changes that was made was:

The default cache directory has been renamed from .cache to .pytest_cache after community feedback that the name .cache did not make it clear that it was used by pytest.

According to the docs, pytest-cache was integrated in pytest 2.8, and the plugin provides two command line options to rerun failures from the last pytest invocation:

  • --lf, --last-failed - to only re-run the failures.
  • --ff, --failed-first - to run the failures first and then the rest of the tests. For cleanup (usually not needed), a --cache-clear option allows to remove all cross-session cache contents ahead of a test run.

Since pytest version 3.8.1 (2018-09-22) .pytest_cache directory contains its own .gitignore file and is automatically ignored by Git. (see more)

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
1

It would appear that the purpose of pytest is to facilitate running only the tests that failed on a previous run. Presumably, this lets you focus your test runs on code that is being actively modified until those tests work, at which point you should clear the cache or use the --ff option to run all the tests again.

chepner
  • 497,756
  • 71
  • 530
  • 681