主頁 > 知識庫 > Python Pytest裝飾器@pytest.mark.parametrize詳解

Python Pytest裝飾器@pytest.mark.parametrize詳解

熱門標簽:外呼并發(fā)線路 地圖標注審核表 宿遷星美防封電銷卡 湛江智能外呼系統(tǒng)廠家 ai電話機器人哪里好 百度地圖標注沒有了 長沙高頻外呼系統(tǒng)原理是什么 ai電銷機器人源碼 西藏房產(chǎn)智能外呼系統(tǒng)要多少錢

Pytest中裝飾器@pytest.mark.parametrize('參數(shù)名',list)可以實現(xiàn)測試用例參數(shù)化,類似DDT
如:@pytest.mark.parametrize('請求方式,接口地址,傳參,預(yù)期結(jié)果',[('get','www.baidu.com','{"page":1}','{"code":0,"msg":"成功"})',('post','www.baidu.com','{"page":2}','{"code":0,"msg":"成功"}')])

1、第一個參數(shù)是字符串,多個參數(shù)中間用逗號隔開

2、第二個參數(shù)是list,多組數(shù)據(jù)用元祖類型;傳三個或更多參數(shù)也是這樣傳。list的每個元素都是一個元組,元組里的每個元素和按參數(shù)順序一一對應(yīng)

3、傳一個參數(shù) @pytest.mark.parametrize('參數(shù)名',list) 進行參數(shù)化

4、傳兩個參數(shù)@pytest.mark.parametrize('參數(shù)名1,參數(shù)名2',[(參數(shù)1_data[0], 參數(shù)2_data[0]),(參數(shù)1_data[1], 參數(shù)2_data[1])]) 進行參數(shù)化

import pytest
#單參數(shù)單值
@pytest.mark.parametrize("user",["18221124104"])
def test(user):
    print(user)
    assert user=="18221124104"
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 1 item
 
test03.py 18221124104
.
 
============================== 1 passed in 0.15s ==============================
 
Process finished with exit code 0
 
#單參數(shù)多值
@pytest.mark.parametrize("user",["18221124104","18200000000","18200000001"])
def test(user):
    print(user)
    assert user=="18221124104"
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items
 
test03.py 18221124104
.18200000000
F18200000001
F
 
================================== FAILURES ===================================
______________________________ test[18200000000] ______________________________
 
user = '18200000000'
 
    @pytest.mark.parametrize("user",["18221124104","18200000000","18200000001"])
    def test(user):
        print(user)
>       assert user=="18221124104"
E       AssertionError
 
test03.py:74: AssertionError
______________________________ test[18200000001] ______________________________
 
user = '18200000001'
 
    @pytest.mark.parametrize("user",["18221124104","18200000000","18200000001"])
    def test(user):
        print(user)
>       assert user=="18221124104"
E       AssertionError
 
test03.py:74: AssertionError
========================= 2 failed, 1 passed in 0.21s =========================
 
Process finished with exit code 0

#多參數(shù)多值
@pytest.mark.parametrize("user,pwd",[("18221124104",111111),("18200000000",111111)])
def test(user,pwd):
    print(user,pwd)
  
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 2 items
 
test03.py 18221124104 111111
.18200000000 111111
.
 
============================== 2 passed in 0.03s ==============================
 
Process finished with exit code 0
 
# 使用內(nèi)置的mark.xfail標記為失敗的用例就不運行了,直接跳過顯示xfailed
@pytest.mark.parametrize("user,pwd",[("18221124104",111111),pytest.param("18200000000",111111,marks=pytest.mark.xfail)])
def test(user,pwd):
    print(user,pwd)
    assert user == "18221124104"
    assert pwd== 111111
  
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 2 items
 
test03.py 18221124104 111111
.18200000000 111111
x
 
======================== 1 passed, 1 xfailed in 0.14s =========================
 
Process finished with exit code 0
 
#若要獲得多個參數(shù)化參數(shù)的所有組合,可以堆疊參數(shù)化裝飾器
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    print("測試數(shù)據(jù)組合:x->%s, y->%s" % (x, y))
 
if __name__=="__main__":
    pytest.main(["-s","test03.py"])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 4 items
 
test03.py 測試數(shù)據(jù)組合:x->0, y->2
.測試數(shù)據(jù)組合:x->1, y->2
.測試數(shù)據(jù)組合:x->0, y->3
.測試數(shù)據(jù)組合:x->1, y->3
.
 
============================== 4 passed in 0.03s ==============================
 
Process finished with exit code 0

到此這篇關(guān)于Python Pytest裝飾器@pytest.mark.parametrize詳解的文章就介紹到這了,更多相關(guān)pytest.mark.parametrize內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python中有函數(shù)重載嗎
  • 在Python中實現(xiàn)函數(shù)重載的示例代碼
  • python裝飾器原理源碼示例分析
  • 理解python中裝飾器的作用
  • 詳解Python裝飾器 給你的咖啡加點料
  • 如何正確理解python裝飾器
  • python 裝飾器的使用與要點
  • 如何利用飾器實現(xiàn) Python 函數(shù)重載

標簽:盤錦 林芝 寧夏 大同 海南 普洱 漯河 南平

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Python Pytest裝飾器@pytest.mark.parametrize詳解》,本文關(guān)鍵詞  Python,Pytest,裝飾,器,@pytest.mark.parametrize,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python Pytest裝飾器@pytest.mark.parametrize詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python Pytest裝飾器@pytest.mark.parametrize詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章