欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年08月23日 11:19:55   作者:王大力測(cè)試進(jìn)階之路  
本文主要介紹了Python Pytest裝飾器@pytest.mark.parametrize詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

1、第一個(gè)參數(shù)是字符串,多個(gè)參數(shù)中間用逗號(hào)隔開(kāi)

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

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

4、傳兩個(gè)參數(shù)@pytest.mark.parametrize('參數(shù)名1,參數(shù)名2',[(參數(shù)1_data[0], 參數(shù)2_data[0]),(參數(shù)1_data[1], 參數(shù)2_data[1])]) 進(jìn)行參數(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標(biāo)記為失敗的用例就不運(yùn)行了,直接跳過(guò)顯示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
 
#若要獲得多個(gè)參數(shù)化參數(shù)的所有組合,可以堆疊參數(shù)化裝飾器
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    print("測(cè)試數(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 測(cè)試數(shù)據(jù)組合:x->0, y->2
.測(cè)試數(shù)據(jù)組合:x->1, y->2
.測(cè)試數(shù)據(jù)組合:x->0, y->3
.測(cè)試數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)OpenCV的安裝與使用示例

    Python實(shí)現(xiàn)OpenCV的安裝與使用示例

    這篇文章主要介紹了Python實(shí)現(xiàn)OpenCV的安裝與使用,結(jié)合實(shí)例形式分析了Python中OpenCV的安裝及針對(duì)圖片的相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Python 變量的創(chuàng)建過(guò)程詳解

    Python 變量的創(chuàng)建過(guò)程詳解

    這篇文章主要介紹了Python 變量的創(chuàng)建過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Python中django學(xué)習(xí)心得

    Python中django學(xué)習(xí)心得

    這篇文章主要介紹了Python中django Web應(yīng)用框架的學(xué)習(xí)做了總結(jié)并把心得體會(huì)寫(xiě)了一下,大家一起參考下吧。
    2017-12-12
  • 如何用Python進(jìn)行回歸分析與相關(guān)分析

    如何用Python進(jìn)行回歸分析與相關(guān)分析

    這篇文章主要介紹了如何用Python進(jìn)行回歸分析與相關(guān)分析,這兩部分內(nèi)容會(huì)放在一起講解,文中提供了解決思路以及部分實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2023-03-03
  • python3利用venv配置虛擬環(huán)境及過(guò)程中的小問(wèn)題小結(jié)

    python3利用venv配置虛擬環(huán)境及過(guò)程中的小問(wèn)題小結(jié)

    這篇文章主要介紹了python3利用venv配置虛擬環(huán)境及過(guò)程中的小問(wèn)題小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • python web框架學(xué)習(xí)筆記

    python web框架學(xué)習(xí)筆記

    這篇文章主要為大家分享了python web框架學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Mac中安裝anaconda并配置虛擬環(huán)境的詳細(xì)過(guò)程

    Mac中安裝anaconda并配置虛擬環(huán)境的詳細(xì)過(guò)程

    這篇文章主要給大家介紹了關(guān)于Mac中安裝anaconda并配置虛擬環(huán)境的詳細(xì)過(guò)程,anaconda是包管理器和環(huán)境管理器,使用它可以方便地創(chuàng)作,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • django多個(gè)APP的urls設(shè)置方法(views重復(fù)問(wèn)題解決)

    django多個(gè)APP的urls設(shè)置方法(views重復(fù)問(wèn)題解決)

    今天小編就為大家分享一篇django多個(gè)APP的urls設(shè)置方法(views重復(fù)問(wèn)題解決),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python中實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法

    python中實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法

    這篇文章主要介紹了python中實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法,代碼很簡(jiǎn)單,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • Python實(shí)現(xiàn)將DNA序列存儲(chǔ)為tfr文件并讀取流程介紹

    Python實(shí)現(xiàn)將DNA序列存儲(chǔ)為tfr文件并讀取流程介紹

    為什么要在實(shí)驗(yàn)過(guò)程中存儲(chǔ)文件,因?yàn)橛行┧惴ǖ膬?nèi)容存在一些重復(fù)計(jì)算的步驟,這些步驟往往消耗很大一部分時(shí)間,在有大量參數(shù)的情況時(shí),需要在多次不同參數(shù)的情況下重復(fù)試驗(yàn),因此可以考慮將一些不涉及參數(shù)運(yùn)算的部分結(jié)果存入文件中
    2022-09-09

最新評(píng)論