這個插件,能夠幫助咱們很好的解決自動化測試過程當中的一些偶線性bug難以復現的問題,但前提是,當前自動化腳本是獨立的,不依賴任何其餘腳本。我的以爲仍是失敗重運行的一種體現,就和TestNG是同樣的,下面咱們來一塊兒感覺下這個插件的使用吧。html
pip3 install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
結合《生成HTML報告插件之pytest-html的使用》這篇文章,仍是結合輸出的html報告來看比較直觀。python
# -*- coding: utf-8 -*- # @Time : 2020/11/29 8:52 # @Author : longrong.lang # @FileName: test_repeat.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang def test_repeat(): import random num = random.randint(1, 9) print(f"\n輸出隨機數:{num}") assert num == 2
使用示例以下:session
# 使用下面哪條命令均可執行 pytest --html=report.html --self-contained-html -s --reruns=5 --count=3 test_repeat.py pytest --html=report.html --self-contained-html -s --reruns=5 --count 3 test_repeat.py
執行效果以下:
dom
生成html報告以下:
測試
注意:插件
使用示例以下:命令行
# 使用下面哪條命令均可執行 pytest --html=report.html --self-contained-html -s --count=3 test_repeat.py pytest --html=report.html --self-contained-html -s --count 3 test_repeat.py
執行效果以下:
3d
很明顯這裏顯示的只是重複執行3次code
這在咱們實際測試中,就很受用了,驗證偶現問題,能夠反覆運行相同的測試腳本直到失敗,能夠將pytest的 -x 選項與pytest-repeat結合使用,以強制測試運行程序在第一次失敗時中止。
使用示例以下:htm
py.test --count=1000 -x test_repeat.py
執行效果以下:
使用 @pytest.mark.repeat(count)標記在測試方法便可,這和TestNg的 @Test(invocationCount = 5)是同樣的。
示例代碼以下:
@pytest.mark.repeat(3) def test_repeat2(): print("\n 測試腳本")
執行效果以下:
命令行參數
做用:能夠覆蓋默認的測試用例執行順序,相似fixture的scope參數
即class中的測試方法,不存在混合狀況,示例代碼以下:
# -*- coding: utf-8 -*- # @Time : 2020/11/29 10:07 # @Author : longrong.lang # @FileName: test_repeatClass.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang class TestRepeatClass1(object): def test_repeat1(self): print("\n repeat 1。。。。。。。。。") class TestRepeatClass2(object): def test_repeat2(self): print("\n repeat 2。。。。。。。。。")
命令行執行:
pytest -s --count=2 --repeat-scope=class test_repeatClass.py
執行效果以下:
能夠理解爲混合,既有類也有單獨的測試方法,示例代碼以下:
# -*- coding: utf-8 -*- # @Time : 2020/11/29 10:07 # @Author : longrong.lang # @FileName: test_repeatClass.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang def test_repeat1(): print("test_repeat1") class TestRepeatClass1(object): def test_repeat1(self): print("\n repeat 1。。。。。。。。。")
執行命令:
pytest -s --count=2 --repeat-scope=moudle test_repeatClass.py
執行效果以下:
兼容性問題
pytest-repeat不能與unittest.TestCase測試類一塊兒使用。不管--count設置多少,這些測試始終僅運行一次,並顯示警告
系列參考文章:
https://www.cnblogs.com/poloyy/category/1690628.html