Python測試框架pytest(16)執行上次失敗用例、檢視與清除快取cache、自定義標記mark
1、執行上次失敗用例
執行全部用例,第一次部分用例執行失敗,此時當被測系統修復後,可執行上次失敗的用例。
命令列輸入 pytest -h
可以查詢到兩個命令列引數:--lf 和 --ff
引數:
-
--lf, --last-failed 只重新執行上次執行失敗的用例(或如果沒有失敗的話會全部跑)。
-
--ff, --failed-first 執行所有測試,但首先執行上次執行失敗的測試(這可能會重新測試,從而導致重複的fixture setup/teardown)。
建立test_lf_ff.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*-""" 微信公眾號:AllTests軟體測試 """ import pytest @pytest.fixture() def open(): name = "AllTests軟體測試" return name def test_case1(open): assert open == "AllTests軟體測試" def test_case2(open): assert open == "AllTests" def test_case3(open2): assert open == "AllTests軟體測試"
開啟命令列,輸入執行指令碼命令:
pytest test_lf_ff.py
執行結果:
第一次執行3個測試用例,1個passed、1個failed、1個error。
1、如果只想執行 failed 和 error 用例,使用引數 --lf
在命令列輸入:
pytest --lf test_lf_ff.py
執行結果:
2、如果想先執行上次失敗的,後執行其它通過的用例,使用引數 --ff
在命令列輸入:
pytest --ff test_lf_ff.py
執行結果:
2、檢視與清除快取cache
pytest 執行完測試用例之後會生成一個 .pytest_cache 的快取資料夾,用於記錄上一次失敗的用例和用例的 ids 等。
pytest 命令列引數:
-
--cache-show=[CACHESHOW] 顯示快取內容,不執行收集用例或測試用例。可選引數:glob(預設值:"*")。
-
--cache-clear 在測試執行開始時刪除所有快取內容。
建立test_cache.py檔案
編寫4條測試用例
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ def test_case1(): assert 0 == 0 def test_case2(): assert 0 == 1 def test_case3(): assert 0 == 0 def test_case4(): assert 0 == 2
命令列輸入執行命令:
pytest test_cache.py
執行結果:
2條用例失敗,2條用例成功。
執行完成後,專案根目錄會生成.pytest_cache的快取資料夾。
目錄結構:
lastfailed檔案記錄之前執行用例為失敗的
可以看到剛執行完的用例,用例2和用例4為失敗的用例。
nodeids檔案記錄之前所有執行用例的節點
2.1、--cache-show
命令列輸入執行命令:
pytest --cache-show
執行結果:
顯示快取內容
2.2、--cache-clear
修改test_cache.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ def test_case5(): assert 0 == 0 def test_case6(): assert 0 == 1 def test_case7(): assert 0 == 0 def test_case8(): assert 0 == 2
命令列輸入執行命令:
pytest test_cache.py
執行結果:
2條用例失敗,2條用例成功。
檢視.pytest_cache的快取資料夾(裡面的檔案記錄是累加的)
lastfailed檔案(包含修改test_cache.py檔案之前執行的失敗用例記錄)
nodeids檔案(包含修改test_cache.py檔案之前執行的用例節點)
使用命令列引數--cache-clear
命令列輸入執行命令:
pytest --cache-clear test_cache.py
執行結果:
執行用例之前,清空所有的快取內容。
再次檢視.pytest_cache的快取資料夾
lastfailed檔案,顯示最新的用例失敗的記錄
nodeids檔案,顯示最新的用例節點
3、自定義標記mark
pytest 可以支援自定義標記,自定義標記可以把一個專案劃分多個模組,然後指定模組名稱執行。
例如:可以標明哪些用例是在 Windows 下執行的,哪些用例是在 Mac 下執行的,在執行程式碼時指定 mark 即可。
示例一:
1、建立test_mark.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ import pytest @pytest.mark.case1 def test_case1(): print("====執行 test_case1====") @pytest.mark.case2 def test_case2(): print("====執行 test_case2====") @pytest.mark.case2 def test_case3(): print("====執行 test_case3====") @pytest.mark.case3 class TestClass: def test_method(self): print("====執行 test_method====") def test_noMark(): print("====沒有標記測試====")
2、開啟命令列,輸入執行命令:
pytest -s -m case1 test_mark.py
執行結果:
只執行標記是case1的(函式test_case1)。
示例二:
還是使用test_mark.py檔案。
如果不想執行標記是case1,其他的都執行,直接取反即可。
開啟命令列,輸入執行命令:
pytest -s -m "not case1" test_mark.py
執行結果:
除了標記是case1的(函式test_case1)沒有執行,其他的標記都執行了。
示例三:
還是使用test_mark.py檔案。
如果想執行多個自定義標記的用例,可以用or
開啟命令列,輸入執行命令:
pytest -s -m "case2 or case1" test_mark.py
執行結果:
執行自定義標記case1、case2。注意:執行的順序,不一定在命令前就先執行。
示例四:
如上面幾個示例,如何避免執行後有warnings資訊。
1、還是使用test_mark.py檔案。之後再建立一個pytest.ini檔案(注意:pytest.ini需要和執行的測試用例同一個目錄,或在根目錄下作用於全域性)。
例如:
檔案內容:
[pytest] markers = case1: 執行case1的測試用例 case2: 執行case2的測試用例 case3: 執行case3的測試用例
2、開啟命令列,輸入執行命令:
pytest -s -m "case2 or case1" test_mark.py
執行結果:
warnings資訊不顯示了。
本文來自部落格園,作者:AllTests軟體測試,轉載請註明原文連結:https://www.cnblogs.com/alltests/p/15458146.html