pytest之.pytest_cache文件夹作用【Pytest中的cache缓存功能】

跑自动化时经常会出现这样一个情况,一轮自动化跑完后零星出现了几个失败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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_test>pytest --cache-show
Test session starts (platform: win32, Python 3.7.0, pytest 5.1.3, pytest-sugar 0.9.2)
rootdir: E:\jenkins\workspace\pytest_junit_report\learnpytest\ping_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:\jenkins\workspace\pytest_junit_report\learnpytest\ping_test\.pytest_cache
-------------------------------------- cache values for '*' ---------------------------------------
cache\lastfailed contains:
  {'product_b/test_smoke.py::test_smoket_1': True}
cache\nodeids 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']
cache\stepwise contains:
  []

 

上一篇:P1150 Peter的烟【递归】


下一篇:ReentrantLock 条件变量简单使用记录