pytest文档26-运行上次失败用例(--lf 和 --ff)

前言

“80%的bug集中在20%的模块,越是容易出现bug的模块,bug是越改越多“平常我们做手工测试的时候,比如用100个用例需要执行,其中10个用例失败了,

当开发修复完bug后,我们一般是重点测上次失败的用例。

那么自动化测试也一样,当用例特别多时,为了节省时间,第一次部分用例失败了,修复完之后,可以只测上次失败的用例。

pytest -h

命令行输入pytest -h,找到里面两个命令行参数: --lf 和 --ff

  • --lf, --last-failed 只重新运行上次运行失败的用例(或如果没有失败的话会全部跑)
  • --ff, --failed-first 运行所有测试,但首先运行上次运行失败的测试(这可能会重新测试,从而导致重复的fixture setup/teardown)

pytest文档26-运行上次失败用例(--lf 和 --ff)

--lf 和 --ff

lf是last-failed的缩写,我第一次运行全部测试用例有4个通过passed, 2个失败failed,1个error

E:\YOYO\web_conf_py>pytest
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 7 items baidu\test_1_baidu.py .. [ 28%]
baidu\test_2.py FF [ 57%]
blog\test_2_blog.py ..E [100%] =================================== ERRORS ====================================
__________________________ ERROR at setup of test_05 __________________________
file E:\YOYO\web_conf_py\blog\test_2_blog.py, line 11
def test_05(start, open_baidu):
E fixture 'open_baidu' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, metadata, monkeypatch, open_blog, pytestconfig, record_property, record_xml_attribute, record_xml_property, recwarn, start, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them. E:\YOYO\web_conf_py\blog\test_2_blog.py:11
================================== FAILURES ===================================
___________________________________ test_06 ___________________________________ start = None, open_baidu = None def test_06(start, open_baidu):
print("测试用例test_01")
> assert 1==2
E assert 1 == 2 baidu\test_2.py:5: AssertionError
---------------------------- Captured stdout call -----------------------------
测试用例test_01
___________________________________ test_07 ___________________________________ start = None, open_baidu = None def test_07(start, open_baidu):
print("测试用例test_02")
> assert 1==2
E assert 1 == 2 baidu\test_2.py:9: AssertionError
---------------------------- Captured stdout call -----------------------------
测试用例test_02
================= 2 failed, 4 passed, 1 error in 0.21 seconds =================

如果只想运行其中2个failed的和1error用例,那么可以直接在cmd输入指令

pytest --lf

E:\YOYO\web_conf_py>pytest --lf
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 7 items / 4 deselected
run-last-failure: rerun previous 3 failures baidu\test_2.py FF [ 66%]
blog\test_2_blog.py E [100%] ========================== 2 failed, 4 deselected, 1 error in 0.16 seconds===========================

如果想先运行上次失败的,后运行其它通过的用例

pytest --ff

E:\YOYO\web_conf_py>pytest --ff
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 7 items
run-last-failure: rerun previous 3 failures first baidu\test_2.py FF [ 28%]
blog\test_2_blog.py E [ 42%]
baidu\test_1_baidu.py .. [ 71%]
blog\test_2_blog.py .. [100%] ================= 2 failed, 4 passed, 1 error in 0.14 seconds =================

---------------------------------pytest结合selenium自动化完整版-------------------------

全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

作者:上海-悠悠 QQ交流群:874033608

也可以关注下我的个人公众号:yoyoketang

pytest文档26-运行上次失败用例(--lf 和 --ff)

上一篇:Android Launcher分析和修改8——AllAPP界面拖拽元素(PagedViewWithDraggableItems)


下一篇:httpclient 优化