日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Pytest学习(十三)- 重复执行之pytest-repeat的使用

發布時間:2023/12/13 综合教程 34 生活家
生活随笔 收集整理的這篇文章主要介紹了 Pytest学习(十三)- 重复执行之pytest-repeat的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面

這個插件,可以幫助我們很好的解決自動化測試過程中的一些偶線性bug難以復現的問題,但前提是,當前自動化腳本是獨立的,不依賴任何其他腳本。個人覺得還是失敗重運行的一種體現,就和TestNG是一樣的,下面我們來一起感受下這個插件的使用吧。

環境準備

py.test版本 ≥ 2.8
Python 2.7、3.4+

安裝插件

pip3 install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

如何使用

結合《生成HTML報告插件之pytest-html的使用》這篇文章,還是結合輸出的html報告來看比較直觀。

1、舉個例子

# -*- 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"
輸出隨機數:{num}")
    assert num == 2

2、結合失敗重跑,并輸出報告

使用示例如下:

# 使用下面哪條命令都可執行
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

執行效果如下:

生成html報告如下:

注意:

reruns=5:意思是失敗重運行5次
count=3:意思是重復執行3次

3、僅重復執行

使用示例如下:

# 使用下面哪條命令都可執行
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

執行效果如下:

很明顯這里顯示的只是重復執行3次

4、重復測試直到失敗

這在我們實際測試中,就很受用了,驗證偶現問題,可以反復運行相同的測試腳本直到失敗,可以將pytest的 -x 選項與pytest-repeat結合使用,以強制測試運行程序在第一次失敗時停止。
使用示例如下:

py.test --count=1000 -x test_repeat.py

執行效果如下:

5、使用注解的形式來實現重復執行

使用 @pytest.mark.repeat(count)標記在測試方法即可,這和TestNg的 @Test(invocationCount = 5)是一樣的。
示例代碼如下:

@pytest.mark.repeat(3)
def test_repeat2():
    print("
 測試腳本")

執行效果如下:

repeat-scope的使用

命令行參數
作用:可以覆蓋默認的測試用例執行順序,類似fixture的scope參數

function:默認,范圍針對每個用例重復執行,再執行下一個用例
class:以class為用例集合單位,重復執行class里面的用例,再執行下一個
module:以模塊為單位,重復執行模塊里面的用例,再執行下一個
session:重復整個測試會話,即所有測試用例的執行一次,然后再執行第二次

1、重復執行class里面的用例

即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("
 repeat 1。。。。。。。。。")

class TestRepeatClass2(object):
    def test_repeat2(self):
        print("
 repeat 2。。。。。。。。。")

命令行執行:

pytest -s --count=2 --repeat-scope=class test_repeatClass.py

執行效果如下:

2、以模塊為單位,重復執行模塊里面的用例

可以理解為混合,既有類也有單獨的測試方法,示例代碼如下:

# -*- 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("
 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

總結

以上是生活随笔為你收集整理的Pytest学习(十三)- 重复执行之pytest-repeat的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。