跑自动化时经常会出现这样一个情况,一轮自动化跑完后零星出现了几个失败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
--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
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: []
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。