學(xué)習(xí)筆記-Pytest(二十)重復(fù)執(zhí)行用例(pytest-repeat)

1.前言


平常在做功能測試的時(shí)候,經(jīng)常會(huì)遇到某個(gè)模塊不穩(wěn)定,偶然會(huì)出現(xiàn)一些bug,對于這種問題我們會(huì)針對此用例反復(fù)執(zhí)行多次,最終復(fù)現(xiàn)出問題來。
自動(dòng)化運(yùn)行用例時(shí)候,也會(huì)出現(xiàn)偶然的bug,可以針對單個(gè)用例,或者針對某個(gè)模塊的用例重復(fù)執(zhí)行多次。

2.pytest-repeat


pytest-repeat是pytest的一個(gè)插件,用于重復(fù)執(zhí)行單個(gè)用例,或多個(gè)測試用例,并指定重復(fù)次數(shù),pytest-repeat支持的版本:

  • Python 2.7, 3.4+ 或 PyPy
  • py.test 2.8或更高
pip3 install pytest-repeat

使用--count命令行選項(xiàng)指定要運(yùn)行測試用例和測試次數(shù)

py.test --count=10 test_file.py

3.重復(fù)執(zhí)行--count


運(yùn)行以下代碼,項(xiàng)目結(jié)構(gòu)如下

web_conf_py是項(xiàng)目工程名稱
│  conftest.py  
│  __init__.py
│              
├─baidu
│  │  conftest.py
│  │  test_1_baidu.py
│  │  test_2.py
│  │  __init__.py 
│          
├─blog
│  │  conftest.py
│  │  test_2_blog.py
│  │  __init__.py      

代碼參考:

# web_conf_py/conftest.py
import pytest

@pytest.fixture(scope="session")
def start():
    print("\n打開首頁")
    return "yoyo"

# web_conf_py/baidu/conftest.py
import pytest

@pytest.fixture(scope="session")
def open_baidu():
    print("打開百度頁面_session")

# web_conf_py/baidu/test_1_baidu.py
import pytest
import time

def test_01(start, open_baidu):
    print("測試用例test_01")
    time.sleep(1)
    assert start == "yoyo"

def test_02(start, open_baidu):
    print("測試用例test_02")
    time.sleep(1)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_1_baidu.py"])


# web_conf_py/baidu/test_2.py
import pytest
import time

def test_06(start, open_baidu):
    print("測試用例test_01")
    time.sleep(1)
    assert start == "yoyo"
def test_07(start, open_baidu):
    print("測試用例test_02")
    time.sleep(1)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_2.py"])

cmd進(jìn)入到工程目錄后,不帶--count參數(shù)只會(huì)執(zhí)行一次,加上參數(shù)--count=5,用例會(huì)重復(fù)執(zhí)行5次

pytest baidu/test_1_baidu.py -s --count=5

從運(yùn)行的用例結(jié)果看,是先重復(fù)5次test_01,再重復(fù)5次test_02,有時(shí)候我們希望執(zhí)行的順序是test_01,test_02按這樣順序重復(fù)五次,接下來就用到一個(gè)參數(shù)--repeat-scope

4.--repeat-scope


--repeat-scope類似于pytest fixturescope參數(shù),--repeat-scope也可以設(shè)置參數(shù): session, module,class或者function(默認(rèn)值)

function(默認(rèn))范圍針對每個(gè)用例重復(fù)執(zhí)行,再執(zhí)行下一個(gè)用例
class 以class為用例集合單位,重復(fù)執(zhí)行class里面的用例,再執(zhí)行下一個(gè)
module 以模塊為單位,重復(fù)執(zhí)行模塊里面的用例,再執(zhí)行下一個(gè)
session 重復(fù)整個(gè)測試會(huì)話,即所有收集的測試執(zhí)行一次,然后所有這些測試再次執(zhí)行等等

使用--repeat-scope=session重復(fù)執(zhí)行整個(gè)會(huì)話用例

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

5.@pytest.mark.repeat(count)


如果要在代碼中標(biāo)記要重復(fù)多次的測試,可以使用@pytest.mark.repeat(count)裝飾器

# test_1_baidu.py
import pytest
import time

def test_01(start, open_baidu):
    print("測試用例test_01")
    time.sleep(0.5)
    assert start == "yoyo"

@pytest.mark.repeat(5)
def test_02(start, open_baidu):
    print("測試用例test_02")
    time.sleep(0.5)
    assert start == "yoyo"

if __name__ == "__main__":
    pytest.main(["-s", "test_1_baidu.py"])

這樣執(zhí)行用例時(shí)候,就不用帶上--count參數(shù),只針對test_02重復(fù)執(zhí)行5次

E:\YOYO\web_conf_py>pytest baidu/test_1_baidu.py -s
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\web_conf_py, inifile:
plugins: xdist-1.23.2, repeat-0.7.0, metadata-1.7.0, html-1.19.0, forked-0.2
collected 6 items

baidu\test_1_baidu.py
打開首頁
打開百度頁面_session
測試用例test_01
.測試用例test_02
.測試用例test_02
.測試用例test_02
.測試用例test_02
.測試用例test_02
.

========================== 6 passed in 3.05 seconds ===========================

6.重復(fù)測試直到失敗


如果您正在嘗試診斷間歇性故障,那么一遍又一遍地運(yùn)行相同的測試直到失敗是有用的。您可以將pytest的-x選項(xiàng)與pytest-repeat結(jié)合使用,以強(qiáng)制測試運(yùn)行器在第一次失敗時(shí)停止。例如:

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

這將嘗試運(yùn)行test_file.py 1000次,但一旦發(fā)生故障就會(huì)停止

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容