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)

--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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM