pytest之@pytest.mark.parametrize實例講解


在測試用例的前面加上:
@pytest.mark.parametrize("參數名",列表數據)
參數名:用來接收每一項數據,並作為測試用例的參數。
列表數據:一組測試數據。

#====參數為列表嵌套字典====
import pytest

def secend(c,b):
    add= int(b)+int(c)
    print(add)
    return add

@pytest.mark.parametrize('case',[{'b':'34','c':'56'},{'b':'39','c':'56'}])
def test_1(case):
    pp=case
    tt=secend(**case)
    print("測試賬號:%s" % tt)


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

運行結果:

============================= test session starts =============================
platform win32 -- Python 3.5.4, pytest-5.1.3, py-1.7.0, pluggy-0.13.1
rootdir: C:\Users\heguanghua\package\untitled_web
plugins: allure-pytest-2.8.5, PyTestReport-0.2.1, forked-1.0.2, html-1.22.0, metadata-1.7.0, rerunfailures-7.0, xdist-1.28.0
collected 2 items

1.py 90
測試賬號:90
.95
測試賬號:95
.

============================== 2 passed in 0.05s ==============================
#====參數為列表嵌套元組====
import pytest
test_datas = [
    (11, 22, 33),
    (22, 33, 55)
]

@pytest.mark.parametrize("data", test_datas)
def test_add02(data):
    res = data[0] + data[1]
    print(res)
    assert res == data[2]

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

運行結果:

============================= test session starts =============================
platform win32 -- Python 3.5.4, pytest-5.1.3, py-1.7.0, pluggy-0.13.1
rootdir: C:\Users\heguanghua\package\untitled_web
plugins: allure-pytest-2.8.5, PyTestReport-0.2.1, forked-1.0.2, html-1.22.0, metadata-1.7.0, rerunfailures-7.0, xdist-1.28.0
collected 2 items

1.py 33
.55
.

============================== 2 passed in 0.05s ==============================
#====參數為列表嵌套列表====
import pytest
test_datas = [
    [11, 22, 33],
    [22, 33, 55]
]

@pytest.mark.parametrize("data", test_datas)
def test_add02(data):
    res = data[0] + data[1]
    print(res)
    assert res == data[2]

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

運行結果:

============================= test session starts =============================
platform win32 -- Python 3.5.4, pytest-5.1.3, py-1.7.0, pluggy-0.13.1
rootdir: C:\Users\heguanghua\package\untitled_web
plugins: allure-pytest-2.8.5, PyTestReport-0.2.1, forked-1.0.2, html-1.22.0, metadata-1.7.0, rerunfailures-7.0, xdist-1.28.0
collected 2 items

1.py 33
.55
.

============================== 2 passed in 0.05s ==============================


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM