有没有办法改变pytest的.cache目录的位置?

non*_*bot 19 python directory caching pytest

我需要能够将pytest的.cache目录的位置更改为env变量WORKSPACE.由于服务器权限超出我的控制范围,我遇到此错误,因为我的用户没有权限在运行测试的目录中写入:

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

有没有办法将.cache目录的路径设置为环境变量WORKSPACE?

vog*_*vog 25

您可以.cache/通过禁用"cacheprovider"插件来阻止创建:

py.test -p no:cacheprovider ...
Run Code Online (Sandbox Code Playgroud)


Sil*_*Guy 10

请参阅每个pytest -hpytest --help以下可用的内置帮助:

如果您更喜欢使用命令行参数,可以使用 a-o--override-ini=...switch:

pytest tests -o cache_dir=.my_cache_dir
Run Code Online (Sandbox Code Playgroud)

或者,从 pytest 3.2 开始,您可以cache_dirpytest配置文件中指定该选项(pytest.ini默认调用):

# pytest.ini
[pytest]
cache_dir = .my_cache_dir
Run Code Online (Sandbox Code Playgroud)

如果您更喜欢自定义配置文件名,则可以使用例如

pytest tests -c .my_pytest.ini
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记在 .ini 文件中提及“[pytest]”部分 (2认同)

Wer*_*ght 7

您可以创建一个pytest.ini在测试的父目录之一中调用的空文件,这些文件将成为将rootdir在其中.cache创建的文件.

请参阅https://pytest.org/latest/customize.html

它不是理想的,但它允许某种形式的定制.