目錄
1、運行上次失敗用例
2、查看與清除快取cache
2.1、--cache-show
2.2、--cache-clear
3、自定義標記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資訊不顯示了,


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336216.html
標籤:其他
