一.pytest.xfail & @pytest.mark.xfail(raises=ErrorType)
1.pytest.xfail: 將該用例標記成xfail失敗,並且該用例中的后續代碼不會執行
在測試用例中調用pytes.xfail()方法,可以選擇傳入reason參數表示原因
import pytest class TestMyPytest: """Pytest learning part one: @pytest.mark.xfail(raises = xx)""" def test_fun1(self): pytest.xfail(reason="no function body") print(f"After the xfail!") assert 1 == 4 def test_fun2(self) -> None: assert True if __name__ == '__main__': pytest.main()
這個方法是我們直接將用例標記為失敗,那什么情況我們會這么做呢?功能未完成、已知有問題。除此之外,就是用例的執行需要前置條件或操作,如果前置條件或操作失敗,那么我們就可以直接將該用例設為失敗,也就是xfail
2.@pytest.mark.xfail標簽
期望測試用例是失敗的,但是不會影響測試用例的的執行。
如果測試用例執行失敗的則結果是xfail,不會額外顯示出錯誤信息;如果測試用例執行成功的則結果是xpass。
import pytest class TestMyPytest: """Pytest learning part one: @pytest.mark.xfail(raises = xx)""" @pytest.mark.xfail(raises=AssertionError) def test_fun1(self) -> None: assert 1 == 2 def test_fun2(self) -> None: assert True if __name__ == '__main__': pytest.main()
測試結果如下:收集了2個測試用例,標記的用例確實運行了;因為斷言失敗所以結果是xfailed,也沒有像正常一樣顯示出錯誤用例及具體信
如果運行成功,運行結果就是xpass.
import pytest class TestMyPytest: """Pytest learning part one: @pytest.mark.xfail(raises = xx)""" @pytest.mark.xfail(raises=AssertionError) def test_fun1(self) -> None: assert 1 == 1 def test_fun2(self) -> None: assert True if __name__ == '__main__':
pytest.main(['-sv', 'test_rais_exce.py'])
二、pytest.mark.repeat()
1.重復執行某個測試用例
背景:某個測試用例甚至整個測試重復執行多次。這時你可能會想到多寫幾次運行函數,再不就寫個for循環。其實pytest提供了一個擴展模塊:pytest-repeat模塊
安裝:pip install pytest-repeat
作用:用@pytest.mark.repeat()標簽,裝飾測試用例,來實現該用例的重復執行多少次。
repeat()中傳入必選參數:重復執行的次數
栗子:
import pytest class TestMyPytest: """Pytest learning part one: @pytest.mark.xfail(raises = xx)""" @pytest.mark.repeat(3) def test_fun1(self): print("fun1 is running...") assert 1 == 1 def test_fun2(self) -> None: assert True if __name__ == '__main__': pytest.main(['-sv', 'test_rais_exce.py'])
運行結果如下:fun1用例被裝飾repeat,執行3次
2.重復執行測試類
直接用@pytest.mark.repeat()標簽裝飾該測試類。
注意:執行順序是第一個用例執行N次,再執行下一個用例N次;而不是每個用例執行一次,共執行N輪。
栗子:
"""Test class repeat n times""" import pytest @pytest.mark.repeat(2) class TestMyPytest: """Test class""" def test_fun1(self): print("fun1 is running...") assert 1 == 1 def test_fun2(self): print("fun2 is running...") assert 2 == 2 if __name__ == '__main__': pytest.main(['-sv', '重復測試類.py'])
運行結果:第一條用例執行2次,再執行第二條用例,以此類推
3.重復執行測試模塊(.py文件)
直接在模塊上方(導入pytest包下面)加入下面一行代碼:pytestmark=pytest.mark.repeat(2)
(pytestmark是固定的)
栗子:
"""Test module repeat n times""" import pytest pytestmark = pytest.mark.repeat(2) class TestMyPytest_1: """Test class""" def test_fun1(self): print("fun1 is running...") assert 1 == 1 def test_fun2(self): print("fun2 is running...") assert 2 == 2 class TestMyPytest_2: """Test Class 2""" def test_fun3(self): print("fun3 is running...") assert 3 == 3 if __name__ == '__main__': pytest.main(['-sv', '重復測試模塊.py'])
運行結果:該模塊.py中的所有類中的測試用例,都被執行了2次。(執行順序也是第一條執行2次,再執行第二條,以此類推)
三、--count 和 --repeat-scope實現重復執行測試
1、--count方式:在運行函數(或命令行)中傳入了參數: --count==執行次數 。
栗子:
"""Test repeat n times""" import pytest class TestMyPytest_1: """Test class""" def test_fun1(self): print("fun1 is running...") assert 1 == 1 def test_fun2(self): print("fun2 is running...") assert 2 == 2 class TestMyPytest_2: """Test Class 2""" def test_fun3(self): print("fun3 is running...") assert 3 == 3 if __name__ == '__main__': pytest.main(['-sv', '--count=2', 'count實現重復執行測試.py'])
運行結果:運行順序還是第一個用例執行N次,再執行下一個用例N次
那么如何讓每個用例都執行一次,共執行n次循環呢?-------–repeat-scope參數
2.–repeat-scope
參數有四個值的選擇范圍:
function:以測試用例為單位,將測試用例重復執行N次;在執行下一個用例
class:以測試類為單位,將類中的每個用例依次執行一遍,共執行N輪;再執行下一個測試類
module:以模塊為單位,將模塊中的每個用例依次執行一遍,共執行N輪;再執行下一個模塊
session:以整個測試會話為單位,將會話中的每個用例依次執行一遍;共執行N輪
栗子:設置--repeat-scope的參數為session
"""Test repeat n times""" import pytest class TestMyPytest_1: """Test class""" def test_fun1(self): print("fun1 is running...") assert 1 == 1 def test_fun2(self): print("fun2 is running...") assert 2 == 2 class TestMyPytest_2: """Test Class 2""" def test_fun3(self): print("fun3 is running...") assert 3 == 3 if __name__ == '__main__': pytest.main(['-sv', '--count=2', '--repeat-scope=session', 'scope參數為session.py'])
運行結果:如下每個用例執行一次,共循環2次
栗子2:--repeat-scope=module,結果同上(一樣的)
栗子3:--repeat-scope=class,結果如下:以類為單位,先運行第一個類中的每個用例,每個運行一次,執行2次;同理再運行第二個類中的用例
栗子4:--repeat-scope=function,結果如下,以function為單位