概述
跑自动化时经常会出现这样一个情况,一轮自动化跑完后零星出现了几个失败case,无法断定失败的原因,所以需要重新跑一下失败的case去debug,那我们要做的是就去修改脚本把那几个case筛选出来重新run。在pytest 中,你就无需这样做,因为cache功能,他能把上次跑的情况记录下来。不知道你们有没有注意到,用pytest run完case后会在当前目录下生成.pytest_cache,里面就保存了上一次run的信息。
跟cache有关的命令参数
--last-failed, 如果run的时候跟这个参数只会运行上次失败的用例,这就解决了上面说的需求。
--failed-first,如果run的时候跟这个参数会先运行上次失败的case,然后再run其余的case。
--cache-show,跟上个参数,会显示上次run的信息。
--cache-clear, 在run前先把之前的cache清除。
例如:
E:jenkinsworkspacepytest_junit_reportlearnpytestping_test>pytest -q --tb=no
Test session starts (platform: win32, Python 3.7.0, pytest 5.1.3, pytest-sugar 0.9.2)
rootdir: E:jenkinsworkspacepytest_junit_reportlearnpytestping_test, inifile: pytest.ini
plugins: allure-pytest-2.8.11, assume-2.2.0, html-2.0.0, metadata-1.8.0, sugar-0.9.2
product_a/test_smoke.py ✓ 14% █▌
product_a/test_smoke.py ✓✓ 29% ██▉
product_a/test_smoke.py ✓✓✓ 43% ████▍
product_b/test_smoke.py ⨯ 57% █████
product_b/test_smoke.py ⨯✓ 71% █████
product_b/test_smoke.py ⨯✓✓ 86% █████
product_b/test_smoke.py ⨯✓✓✓ 100% █████
█████
Results (0.12s):
6 passed
1 failed
- product_b/test_smoke.py:1 test_smoket_1
一共七个case,一个failed。
–last-failed
E:jenkinsworkspacepytest_junit_reportlearnpytestping_test>pytest --tb=no --lf
Test session starts (platform: win32, Python 3.7.0, pytest 5.1.3, pytest-sugar 0.9.2)
rootdir: E:jenkinsworkspacepytest_junit_reportlearnpytestping_test, inifile: pytest.ini
plugins: allure-pytest-2.8.11, assume-2.2.0, html-2.0.0, metadata-1.8.0, sugar-0.9.2
collecting ... run-last-failure: rerun previous 1 failure (skipped 9 files)
product_b/test_smoke.py ⨯ 100% █████
█████
Results (0.07s):
1 failed
- product_b/test_smoke.py:1 test_smoket_1
3 deselected
这里只run了上一次失败的case
–failed-first
E:jenkinsworkspacepytest_junit_reportlearnpytestping_test>pytest --tb=no --ff
Test session starts (platform: win32, Python 3.7.0, pytest 5.1.3, pytest-sugar 0.9.2)
rootdir: E:jenkinsworkspacepytest_junit_reportlearnpytestping_test, inifile: pytest.ini
plugins: allure-pytest-2.8.11, assume-2.2.0, html-2.0.0, metadata-1.8.0, sugar-0.9.2
collecting ... run-last-failure: rerun previous 1 failure first
product_b/test_smoke.py ⨯ 14% █▌
product_a/test_smoke.py ✓ 29% ██▉
product_a/test_smoke.py ✓✓ 43% ████▍
product_b/test_smoke.py ⨯✓✓✓ 100% █████
█████
Results (0.11s):
6 passed
1 failed
- product_b/test_smoke.py:1 test_smoket_1
这里先会run之前失败的case 然后在run其他的。
–cache-show
E:jenkinsworkspacepytest_junit_reportlearnpytestping_test>pytest --cache-show
Test session starts (platform: win32, Python 3.7.0, pytest 5.1.3, pytest-sugar 0.9.2)
rootdir: E:jenkinsworkspacepytest_junit_reportlearnpytestping_test, inifile: pytest.ini
plugins: allure-pytest-2.8.11, assume-2.2.0, html-2.0.0, metadata-1.8.0, sugar-0.9.2
cachedir: E:jenkinsworkspacepytest_junit_reportlearnpytestping_test.pytest_cache
-------------------------------------- cache values for '*' ---------------------------------------
cachelastfailed contains:
{'product_b/test_smoke.py::test_smoket_1': True}
cachenodeids contains:
['product_a/test_smoke.py::test_smoket_1',
'product_a/test_smoke.py::test_smoket_2',
'product_a/test_smoke.py::test_smoket_3',
'product_b/test_smoke.py::test_smoket_1',
'product_b/test_smoke.py::test_smoket_2',
'product_b/test_smoke.py::test_smoket_3',
'product_b/test_smoke.py::test_smoket_4']
cachestepwise contains:
[]
最后
以上就是过时犀牛为你收集整理的pytest之.pytest_cache文件夹作用【Pytest中的cache缓存功能】的全部内容,希望文章能够帮你解决pytest之.pytest_cache文件夹作用【Pytest中的cache缓存功能】所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复