目錄
- 1、前言
- 2、--count引數使用
- (一)在命令列或者
main函式使用 - (二)在全域組態檔中使用
- (一)在命令列或者
- 3、--repeat-scope引數使用
- 4、@pytest.mark.repeat(count)裝飾器使用
- 5、結合引數x使重復執行在失敗時停止
1、前言
在自動化測驗的時候我們可能會遇到某些原因,如模塊不穩定等,出現一些測驗失敗,此時我們想要針對單個用例或者單個模塊重復執行多次,以確定測驗失敗的真正原因,在Pytest中可以通過插件pytest-repeat來實作,
安裝方式:pip install pytest-repeat
2、--count引數使用
(一)在命令列或者main函式使用
pytest -s -v ./xxx.py --count=3
pytest.main(["-vs","xxx.py","--count=3"])
示例:
def test_1():
print("測驗1")
assert True
def test_2():
print("測驗2")
assert False
"""
執行結果
mark/repeat/repeat_count.py::test_1[1-3] 測驗1
PASSED
mark/repeat/repeat_count.py::test_1[2-3] 測驗1
PASSED
mark/repeat/repeat_count.py::test_1[3-3] 測驗1
PASSED
mark/repeat/repeat_count.py::test_2[1-3] 測驗2
FAILED
mark/repeat/repeat_count.py::test_2[2-3] 測驗2
FAILED
mark/repeat/repeat_count.py::test_2[3-3] 測驗2
FAILED
"""
(二)在全域組態檔中使用
在pytest.ini組態檔中addopts添加count引數
[pytest]
addopts = -s -v --count 2
testpaths = scripts
python_files = test_*.py
python_classes = Test*
python_functions = test*
示例:
def test_1():
print("測驗1")
assert True
def test_2():
print("測驗2")
assert False
"""
執行結果
mark/repeat/repeat_count.py::test_1[1-2] 測驗1
PASSED
mark/repeat/repeat_count.py::test_1[2-2] 測驗1
PASSED
mark/repeat/repeat_count.py::test_2[1-2] 測驗2
FAILED
mark/repeat/repeat_count.py::test_2[2-2] 測驗2
FAILED
"""
3、--repeat-scope引數使用
從上面的示例中可以看到測驗1先執行了3次,然后測驗2再執行3次,有些時候我們想整體執行3次,這就需要用到--repeat-scope引數,類似于fixture的scope也可以設定范圍
session:重復整個測驗會話,即所有收集到的測驗用例測驗完成一次,再進行下一次測驗module:重復整個模塊,即模塊內所有收集到的測驗用例測驗完成一次,再進行下一次測驗class:重復所有類,即以每個類為集合所有測驗用例測驗完成一次,再進行下一次測驗function:默認范圍,重復所有函式和方法,即一個測驗函式或方法重復執行完畢,再對下一個測驗函式重復執行
測驗代碼:
class TestClass_1:
def test_method_1(self):
print("類1測驗方法1")
def test_method_2(self):
print("類1測驗方法2")
class TestClass_2:
def test_method_1(self):
print("類2測驗方法1")
def test_method_2(self):
print("類2測驗方法2")
- 類級別執行
pytest -s -v xxx.py --count=2 --repeat_scope=class
"""
執行結果
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[1-2] 類1測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[1-2] 類1測驗方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[2-2] 類1測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[2-2] 類1測驗方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[1-2] 類2測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[1-2] 類2測驗方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[2-2] 類2測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[2-2] 類2測驗方法2
PASSED
"""
- 模塊級別執行
pytest -s -v xxx.py --count=2 --repeat-scope=module
"""
執行結果
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[1-2] 類1測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[1-2] 類1測驗方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[1-2] 類2測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[1-2] 類2測驗方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_1[2-2] 類1測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_1::test_method_2[2-2] 類1測驗方法2
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_1[2-2] 類2測驗方法1
PASSED
mark/repeat/repeat_scope.py::TestClass_2::test_method_2[2-2] 類2測驗方法2
PASSED
"""
4、@pytest.mark.repeat(count)裝飾器使用
還可以在測驗代碼中使用@pytest.mark.repeat(count)裝飾器來標記需要重復執行的測驗,
import pytest
@pytest.mark.repeat(2)
def test_1():
print("測驗函式1")
def test_2():
print("測驗函式2")
class TestClass:
def test_1(self):
print("類中測驗方法1")
@pytest.mark.repeat(3)
def test_2(self):
print("類中測驗方法2")
"""
執行結果
mark/repeat/repeat_mark.py::test_1[1-2] 測驗函式1
PASSED
mark/repeat/repeat_mark.py::test_1[2-2] 測驗函式1
PASSED
mark/repeat/repeat_mark.py::test_2 測驗函式2
PASSED
mark/repeat/repeat_mark.py::TestClass::test_1 類中測驗方法1
PASSED
mark/repeat/repeat_mark.py::TestClass::test_2[1-3] 類中測驗方法2
PASSED
mark/repeat/repeat_mark.py::TestClass::test_2[2-3] 類中測驗方法2
PASSED
mark/repeat/repeat_mark.py::TestClass::test_2[3-3] 類中測驗方法2
PASSED
"""
注意:使用裝飾器標記的重復執行用例不受引數執行的影響,即當上面例子在執行時使用--count=5,標記過的測驗用例以裝飾器標記次數為準,其他未標記的測驗用例會執行5次,
5、結合引數x使重復執行在失敗時停止
如果遇到間歇性bug,可以在命令中--count與-x結合使用,重復執行測驗用例,直到測驗失敗停止,
pytest -vs -x xxx.py --count=100
這樣將運行100次,一旦遇到失敗就會停止,
參考:https://www.cnblogs.com/yoyoketang/p/9800961.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/502374.html
標籤:Python
上一篇:day23--Java集合06
下一篇:字典(dict)
