我是靠谱客的博主 过时犀牛,这篇文章主要介绍pytest之.pytest_cache文件夹作用【Pytest中的cache缓存功能】,现在分享给大家,希望可以做个参考。

跑自动化时经常会出现这样一个情况,一轮自动化跑完后零星出现了几个失败case,无法断定失败的原因,所以需要重新跑一下失败的case去debug,那我们要做的是就去修改脚本把那几个case筛选出来重新run。在pytest 中,你就无需这样做,因为cache功能,他能把上次跑的情况记录下来。不知道你们有没有注意到,用pytest run完case后会在当前目录下生成.pytest_cache,里面就保存了上一次run的信息。

跟cache有关的命令参数

复制代码
1
2
3
4
5
--last-failed, 如果run的时候跟这个参数只会运行上次失败的用例,这就解决了上面说的需求。 --failed-first,如果run的时候跟这个参数会先运行上次失败的case,然后再run其余的case。 --cache-show,跟上个参数,会显示上次run的信息。 --cache-clear, 在run前先把之前的cache清除。

例如:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部