parametrize参数化对两组数据全排列组合测试

【原文链接】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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值