pytest--参数化parametrize
前言
pytest.mark.parametrize装饰器可以实现测试用例参数化。
parametrizing
1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子
# coding:utf-8
import pytest
@pytest.mark.parametrize("test_input,expect",
[("3+5",8),("2+4",6),("6*9",42),])
def test_params(test_input,expect):
assert eval(test_input)==expect
if __name__=="__main__":
pytest.main(["-s","test_fix1.py"])
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items
test_f1.py ..F
test_f1.py:2 (test_params[6*9-42])
54 != 42
Expected :42
Actual :54
<Click to see difference>
test_input = '6*9', expect = 42
@pytest.mark.parametrize("test_input,expect",
[("3+5",8),("2+4",6),("6*9",42),])
def test_params(test_input,expect):
> assert eval(test_input)==expect
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
test_f1.py:6: AssertionError
Assertion failed
[100%]
================================== FAILURES ===================================
_____________________________ test_params[6*9-42] _____________________________
test_input = '6*9', expect = 42
@pytest.mark.parametrize("test_input,expect",
[("3+5",8),("2+4",6),("6*9",42),])
def test_params(test_input,expect):
> assert eval(test_input)==expect
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
test_f1.py:6: AssertionError
=========================== short test summary info ===========================
FAILED test_f1.py::test_params[6*9-42] - AssertionError: assert 54 == 42
========================= 1 failed, 2 passed in 0.26s =========================
Process finished with exit code 0
Assertion failed
Assertion failed
在这个例子中设计的,只有一条输入/输出值的简单测试功能。和往常一样函数的参数,你可以在运行结果看到在输入和输出值。
2.它也可以标记单个测试实例在参数化,例如使用内置的mark.xfail
# coding:utf-8
import pytest
@pytest.mark.parametrize("test_input,expect",
[("3+5",8),
pytest.param("2+4",6,marks=pytest.mark.xfail),
pytest.param("6*9",42,marks=pytest.mark.xfail),])
def test_params(test_input,expect):
print("-------开始执行测试用例-------")
print(f"test_input:{test_input}")
print(f"expect:{expect}")
assert eval(test_input)==expect
if __name__=="__main__":
pytest.main(["-s","test_fix1.py"])
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items
test_f1.py [100%]
=================== 1 passed, 1 xfailed, 1 xpassed in 0.25s ===================
Process finished with exit code 0
.-------开始执行测试用例-------
test_input:3+5
expect:8
X-------开始执行测试用例-------
test_input:2+4
expect:6
x-------开始执行测试用例-------
test_input:6*9
expect:42
test_input = '6*9', expect = 42
@pytest.mark.parametrize("test_input,expect",
[("3+5",8),
pytest.param("2+4",6,marks=pytest.mark.xfail),
pytest.param("6*9",42,marks=pytest.mark.xfail),])
def test_params(test_input,expect):
print("-------开始执行测试用例-------")
print(f"test_input:{test_input}")
print(f"expect:{expect}")
> assert eval(test_input)==expect
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
test_f1.py:11: AssertionError
标记为失败的用例,预期结果是失败,实际运行也是失败,显示xfailed
参数组合
1.若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器
# coding:utf-8
import pytest
@pytest.mark.parametrize("x",[0,1])
@pytest.mark.parametrize("y",[2,3])
def test_a(x,y):
#print("测试数据组合:x—>%s,y—>%s"%(x,y))
print("测试参数组合,x:{},y:{}".format(x,y))
if __name__=="__main__":
pytest.main(["-s","test_fix1.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 4 items
test_f1.py [100%]
============================== 4 passed in 0.13s ==============================
Process finished with exit code 0
.测试参数组合,x:0,y:2
.测试参数组合,x:1,y:2
.测试参数组合,x:0,y:3
.测试参数组合,x:1,y:3
如下是多参数?
# coding:utf-8
import pytest
@pytest.mark.parametrize("x,y",([0,1],
[1,2],
[2,3]))
def test_a(x,y):
#print("测试数据组合:x—>%s,y—>%s"%(x,y))
print("测试参数组合,x:{},y:{}".format(x,y))
if __name__=="__main__":
pytest.main(["-s","test_fix1.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items
test_f1.py [100%]
============================== 3 passed in 0.12s ==============================
Process finished with exit code 0
.测试参数组合,x:0,y:1
.测试参数组合,x:1,y:2
.测试参数组合,x:2,y:3

浙公网安备 33010602011771号