【原文链接】parametrize参数化对两组数据全排列组合测试
Parametrize参数化同样支持对两组数据的全排列组合测试,这个功能对一些要求数据进行全排列覆盖测试的场景是非常有用的,具体用法如下,即使用两次参数化声明即可。
import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
print("x:",x)
print("y:",y)
assert x<y
执行结果如下,这里可以看出,x和y分别进行了全排列组合,即(0,2),(0,3),(1,2),(1,3),执行结果也明确显示这里有四个测试用例。
(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 4 items
test_demo.py x: 0
y: 2
.x: 1
y: 2
.x: 0
y: 3
.x: 1
y: 3
.
==================== 4 passed in 0.02s ====================
(demo-HCIhX0Hq) E:\demo>
那么加入一组数据是单个类型,另一组数据是多个,比如两个类型的,这两组数据放在一起进行全排列组合,具体使用方法见如下代码。
import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y,z", [(2,3), (3,4)])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
assert x+y==z
执行结果如下,可以使用方式基本是类似的。
(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 4 items
test_demo.py x: 0
y: 2
z: 3
Fx: 1
y: 2
z: 3
.x: 0
y: 3
z: 4
Fx: 1
y: 3
z: 4
.
======================== FAILURES =========================
_____________________ test_foo[2-3-0] _____________________
x = 0, y = 2, z = 3
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y,z", [(2,3), (3,4)])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (0 + 2) == 3
test_demo.py:9: AssertionError
_____________________ test_foo[3-4-0] _____________________
x = 0, y = 3, z = 4
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y,z", [(2,3), (3,4)])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (0 + 3) == 4
test_demo.py:9: AssertionError
================= short test summary info =================
FAILED test_demo.py::test_foo[2-3-0] - assert (0 + 2) == 3
FAILED test_demo.py::test_foo[3-4-0] - assert (0 + 3) == 4
=============== 2 failed, 2 passed in 0.08s ===============
(demo-HCIhX0Hq) E:\demo>
同理假如有三组数据需要进行全排列组合,有怎么操作的,类似地,只需要再增加一个个参数化的声明即可,如下。
import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
assert x+y==z
此时执行结果如下,可以发现,此时的组合个数为222=8个,因此总共有8个用例。
(demo-HCIhX0Hq) E:\demo>pytest -s
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo
plugins: assume-2.4.3, rerunfailures-10.2
collected 8 items
test_demo.py x: 0
y: 2
z: 4
Fx: 1
y: 2
z: 4
Fx: 0
y: 3
z: 4
Fx: 1
y: 3
z: 4
.x: 0
y: 2
z: 5
Fx: 1
y: 2
z: 5
Fx: 0
y: 3
z: 5
Fx: 1
y: 3
z: 5
F
======================== FAILURES =========================
_____________________ test_foo[4-2-0] _____________________
x = 0, y = 2, z = 4
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (0 + 2) == 4
test_demo.py:10: AssertionError
_____________________ test_foo[4-2-1] _____________________
x = 1, y = 2, z = 4
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (1 + 2) == 4
test_demo.py:10: AssertionError
_____________________ test_foo[4-3-0] _____________________
x = 0, y = 3, z = 4
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (0 + 3) == 4
test_demo.py:10: AssertionError
_____________________ test_foo[5-2-0] _____________________
x = 0, y = 2, z = 5
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (0 + 2) == 5
test_demo.py:10: AssertionError
_____________________ test_foo[5-2-1] _____________________
x = 1, y = 2, z = 5
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (1 + 2) == 5
test_demo.py:10: AssertionError
_____________________ test_foo[5-3-0] _____________________
x = 0, y = 3, z = 5
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (0 + 3) == 5
test_demo.py:10: AssertionError
_____________________ test_foo[5-3-1] _____________________
x = 1, y = 3, z = 5
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])
def test_foo(x, y,z):
print("x:",x)
print("y:",y)
print("z:",z)
> assert x+y==z
E assert (1 + 3) == 5
test_demo.py:10: AssertionError
================= short test summary info =================
FAILED test_demo.py::test_foo[4-2-0] - assert (0 + 2) == 4
FAILED test_demo.py::test_foo[4-2-1] - assert (1 + 2) == 4
FAILED test_demo.py::test_foo[4-3-0] - assert (0 + 3) == 4
FAILED test_demo.py::test_foo[5-2-0] - assert (0 + 2) == 5
FAILED test_demo.py::test_foo[5-2-1] - assert (1 + 2) == 5
FAILED test_demo.py::test_foo[5-3-0] - assert (0 + 3) == 5
FAILED test_demo.py::test_foo[5-3-1] - assert (1 + 3) == 5
=============== 7 failed, 1 passed in 0.13s ===============
(demo-HCIhX0Hq) E:\demo>