config.cache API

The pytest-cache plugin adds a config.cache object during the configure-initialization of pytest. This allows other plugins, including conftest.py files, to safely and flexibly store and retrieve values across test runs because the config object is available in many places.

Under the hood, the cache plugin uses the simple dumps/loads API of the cross-interpreter execnet communication library. It makes it safe to store values e. g. under Python2 and retrieve it later from a Python3 or PyPy interpreter.

Cache.get(key, default)[source]

return cached value for the given key. If no value was yet cached or the value cannot be read, the specified default is returned.

Parameters:
  • key – must be a / separated value. Usually the first name is the name of your plugin or your application.
  • default – must be provided in case of a cache-miss or invalid cache values.
Cache.set(key, value)[source]

save value for the given key.

Parameters:
  • key – must be a / separated value. Usually the first name is the name of your plugin or your application.
  • value – must be of any combination of basic python types, including nested types like e. g. lists of dictionaries.
Cache.makedir(name)[source]

return a directory path object with the given name. If the directory does not yet exist, it will be created. You can use it to manage files likes e. g. store/retrieve database dumps across test sessions.

Parameters:name – must be a string not containing a / separator. Make sure the name contains your plugin or application identifiers to prevent clashes with other cache users.

Previous topic

pytest-cache: working with cross-testrun state

This Page