pytest——重復執行


一、重復執行用例repeat

1.安裝:pip install pytest-repeat

2.執行:pytest  test01.py --count=5

1 def test_a():
2     print('i love you python')
3 
4 def test_b():
5     a='hello'
6     assert 'h' in a,'恭喜你答錯了'

platform win32 -- Python 3.7.2, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: D:\PycharmProjects\haha\pytest_learn
plugins: allure-pytest-2.7.0, forked-1.0.2, html-1.21.1, metadata-1.8.0, repeat-0.8.0, xdist-1.29.0
collected 10 items

 
         

test_01.py i love you python
.i love you python
.i love you python
.i love you python
.i love you python
......

 
         

=============================================== 10 passed in 0.03 seconds ================================================

3.也可以設置模塊或者類等執行次數:--repeat-scope,類似於fixture的scope,可以設置參數session,module,class,function(默認值)

1 pytest baidu/test01.py -s --count=5 --repeat-scope=session

4.@pytest.mark.repeat(count),直接標記某個用例執行多少次

1 import pytest
2 
3 @pytest.mark.repeat(5)              #test_a用例執行5次
4 def test_a():
5     print('i love you python')
6 
7 def test_b():
8     a='hello'
9     assert 'h' in a,'恭喜你答錯了'

5.有時候,功能模塊不穩定,間接性的出現錯誤,可以將-x和pytest-repeat一起用,強制運行器在第一次測試失敗時停止:

1 pytest -s --count=1000 -x test_file.py

這將嘗試運行test_file.py 1000次,但一旦發生故障就會停止

 


免責聲明!

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



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