1. 程式人生 > >Pytest學習(十三)- 重複執行之pytest-repeat的使用

Pytest學習(十三)- 重複執行之pytest-repeat的使用

## 寫在前面 這個外掛,可以幫助我們很好的解決自動化測試過程中的一些偶線性bug難以復現的問題,但前提是,當前自動化指令碼是獨立的,不依賴任何其他指令碼。個人覺得還是失敗重執行的一種體現,就和TestNG是一樣的,下面我們來一起感受下這個外掛的使用吧。 ## 環境準備 - py.test版本 ≥ 2.8 - Python 2.7、3.4+ ## 安裝外掛 ```cmd pip3 install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com ``` ## 如何使用 結合[《生成HTML報告外掛之pytest-html的使用》](https://www.cnblogs.com/longronglang/p/14039231.html)這篇文章,還是結合輸出的html報告來看比較直觀。 ### 1、舉個例子 ```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 ``` ### 2、結合失敗重跑,並輸出報告 使用示例如下: ```cmd # 使用下面哪條命令都可執行 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 ``` 執行效果如下: ![](https://img2020.cnblogs.com/blog/718867/202011/718867-20201129103418417-164157627.png) 生成html報告如下: ![](https://img2020.cnblogs.com/blog/718867/202011/718867-20201129103448880-964316246.png) 注意: - reruns=5:意思是失敗重執行5次 - count=3:意思是重複執行3次 ### 3、僅重複執行 使用示例如下: ```cmd # 使用下面哪條命令都可執行 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 ``` 執行效果如下: ![](https://img2020.cnblogs.com/blog/718867/202011/718867-20201129095047650-128169109.png) 很明顯這裡顯示的只是重複執行3次 ### 4、重複測試直到失敗 這在我們實際測試中,就很受用了,驗證偶現問題,可以反覆執行相同的測試指令碼直到失敗,可以將pytest的 -x 選項與pytest-repeat結合使用,以強制測試執行程式在第一次失敗時停止。 使用示例如下: ```python py.test --count=1000 -x test_repeat.py ``` 執行效果如下: ![](https://img2020.cnblogs.com/blog/718867/202011/718867-20201129095707085-1217279173.png) ### 5、使用註解的形式來實現重複執行 使用 @pytest.mark.repeat(count)標記在測試方法即可,這和TestNg的 @Test(invocationCount = 5)是一樣的。 示例程式碼如下: ```python @pytest.mark.repeat(3) def test_repeat2(): print("\n 測試指令碼") ``` 執行效果如下: ![](https://img2020.cnblogs.com/blog/718867/202011/718867-20201129100410011-765457316.png) ## repeat-scope的使用 命令列引數 **作用:**可以覆蓋預設的測試用例執行順序,類似fixture的scope引數 - function:預設,範圍針對每個用例重複執行,再執行下一個用例 - class:以class為用例集合單位,重複執行class裡面的用例,再執行下一個 - module:以模組為單位,重複執行模組裡面的用例,再執行下一個 - session:重複整個測試會話,即所有測試用例的執行一次,然後再執行第二次 ### 1、重複執行class裡面的用例 即class中的測試方法,不存在混合情況,示例程式碼如下: ```python # -*- 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。。。。。。。。。") ``` 命令列執行: ```cmd pytest -s --count=2 --repeat-scope=class test_repeatClass.py ``` 執行效果如下: ![](https://img2020.cnblogs.com/blog/718867/202011/718867-20201129101554514-17287679.png) ### 2、以模組為單位,重複執行模組裡面的用例 可以理解為混合,既有類也有單獨的測試方法,示例程式碼如下: ```python # -*- 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。。。。。。。。。") ``` 執行命令: ```cmd pytest -s --count=2 --repeat-scope=moudle test_repeatClass.py ``` 執行效果如下: ![](https://img2020.cnblogs.com/blog/718867/202011/718867-20201129102633917-394284469.png) **相容性問題** pytest-repeat不能與unittest.TestCase測試類一起使用。無論--count設定多少,這些測試始終僅執行一次,並顯示警告 系列參考文章: https://www.cnblogs.com/poloyy/category/16906