nonbot
nonbot

Reputation: 983

Is there a way to change the location of pytest's .cache directory?

I need to be able to change the location of pytest's .cache directory to the env variable, WORKSPACE. Due to server permissions out of my control, I am running into this error because my user does not have permission to write in the directory where the tests are being run from:

py.error.EACCES: [Permission denied]: open('/path/to/restricted/directory/tests/.cache/v/cache/lastfailed', 'w')

Is there a way to set the path of the .cache directory to the environment variable WORKSPACE?

Upvotes: 33

Views: 20739

Answers (5)

Anton Ovsyannikov
Anton Ovsyannikov

Reputation: 1200

pytest.ini

[pytest]
addopts = -p no:cacheprovider

Upvotes: 0

SilentGuy
SilentGuy

Reputation: 2203

See built-in help available per pytest -h or pytest --help:

If you prefer to use a command line argument, you can use a -o or --override-ini=... switch:

pytest tests -o cache_dir=.my_cache_dir

Alternatively, starting from pytest 3.2, you can specify the cache_dir option in the pytest configuration file (called pytest.ini by default):

# pytest.ini
[pytest]
cache_dir = .my_cache_dir

If you prefer a custom configuration file name, you may use e.g.

pytest tests -c .my_pytest.ini

Upvotes: 31

vardaofthevalier
vardaofthevalier

Reputation: 21

There is no explicit option to change the cache directory on the command line, but it is possible to override the options in pytest.ini with the -o option instead:

pytest -o cache_dir=$WORKSPACE ...

See the output of pytest --help for more info about the -o option.

Also, for reference I'm using pytest 3.7.1.

Upvotes: 1

vog
vog

Reputation: 25627

You can prevent the creation of .cache/ by disabling the "cacheprovider" plugin:

py.test -p no:cacheprovider ...

Upvotes: 34

Wernight
Wernight

Reputation: 37668

You can create an empty file called pytest.ini in one of the parent directories of your test, are that will become the rootdir in which the .cache will be created.

See https://pytest.org/latest/customize.html

It's not ideal but it allows some form of customization.

Upvotes: 8

Related Questions