Python Pytest Decorator @ pytest.mark.parametrize Detailed Explanation

  • 2021-11-24 02:03:17
  • OfStack

The decorator @ pytest. mark. parametrize ('parameter name ', list) in Pytest can parameterize test cases, similar to DDT
For example: @ pytest. mark. parametrize ('Request Mode, Interface Address, Reference, Expected Result', [('get', 'www. baidu. com', '{"page": 1}', '{"code": 0,' msg ":" Success "}) ', (' post ',' www. baidu. com ',' {" page ": 2} ',' {" code ": 0, 'msg": Success})])

1. The first parameter is a string, and multiple parameters are separated by commas

2. The second parameter is list, and multiple groups of data use Yuan ancestor type; Pass 3 or more parameters in the same way. Each element of list is a tuple, and each element in the tuple corresponds to 11 in the order of parameters

3. Pass a parameter @ pytest. mark. parametrize ('Parameter Name', list) for parameterization

4. Pass two parameters @ pytest. mark. parametrize ('Parameter Name 1, Parameter Name 2', [(Parameter 1_data [0], Parameter 2_data [0]), (Parameter 1_data [1], Parameter 2_data [1])]) for parameterization


import pytest
# Single parameter and single value 
@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
 
# Single parameter multi-value 
@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

# Multi-parameter and multi-value 
@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
 
#  Use the built-in mark.xfail Use cases marked as failures are not run, and the display is skipped directly 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
 
# To get all combinations of multiple parameterized parameters, you can stack parameterized decorators 
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    print(" Test data combination: 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  Test data combination: x->0, y->2
. Test data combination: x->1, y->2
. Test data combination: x->0, y->3
. Test data combination: x->1, y->3
.
 
============================== 4 passed in 0.03s ==============================
 
Process finished with exit code 0

Related articles: