五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

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

發(fā)布日期:2022-02-07 08:51 | 文章來源:gibhub

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

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

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

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

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

import pytest
#單參數單值
@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
 
#單參數多值
@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
#多參數多值
@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
 
# 使用內置的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
 
#若要獲得多個參數化參數的所有組合,可以堆疊參數化裝飾器
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
 print("測試數據組合: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 測試數據組合:x->0, y->2
.測試數據組合:x->1, y->2
.測試數據組合:x->0, y->3
.測試數據組合:x->1, y->3
.
 
============================== 4 passed in 0.03s ==============================
 
Process finished with exit code 0

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

海外服務器租用

版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯系alex-e#qq.com處理。

相關文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部