12、pytest -- 缓存:记录执行的状态
目录
pytest
会将本轮测试的执行状态写入到.pytest_cache
文件夹,这个行为是由自带的cacheprovider
插件来实现的;
注意:
pytest
默认将测试执行的状态写入到根目录中的.pytest_cache
文件夹,我们也可以通过在pytest.ini
中配置cache_dir
选项来自定义缓存的目录,它可以是相对路径,也可以是绝对路径;相对路径指的是相对于
pytest.ini
文件所在的目录;例如,我们把这一章的缓存和源码放在一起:在
src/chapter-12/pytest.ini
中添加如下配置:
[pytest] cache_dir = .pytest-cache
这样,即使我们在项目的根目录下执行
src/chapter-12/
中的用例,也只会在pytest-chinese-doc/src/chapter-12/.pytest_cache
中生成缓存,而不再是pytest-chinese-doc/.pytest_cache
中;pytest-chinese-doc (5.1.3) λ pipenv run pytest src/chapter-12
1. cacheprovider
插件
在介绍这个插件之前,我们先看一个简单例子:
# src/chapter-12/test_failed.py import pytest @pytest.mark.parametrize('num', [1, 2]) def test_failed(num):