测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Pytest----parametrize参数化对两组数据全排列组合测试
收藏本文
作者:redrose2100 类别: 日期:2022-12-02 07:26:23 阅读:903 次 消耗积分:0 分
[TOC] ![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/7cd47362-951c-11ee-986a-0242ac110004.png) Parametrize参数化同样支持对两组数据的全排列组合测试,这个功能对一些要求数据进行全排列覆盖测试的场景是非常有用的,具体用法如下,即使用两次参数化声明即可。 ```python 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
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> ``` 那么加入一组数据是单个类型,另一组数据是多个,比如两个类型的,这两组数据放在一起进行全排列组合,具体使用方法见如下代码。 ```python 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 ``` 执行结果如下,可以使用方式基本是类似的。 ```bash (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> ``` 同理假如有三组数据需要进行全排列组合,有怎么操作的,类似地,只需要再增加一个个参数化的声明即可,如下。 ```python 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 ``` 此时执行结果如下,可以发现,此时的组合个数为2*2*2=8个,因此总共有8个用例。 ```bash (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> ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/461
上一篇:
LTP----基于ARM架构的openEuler系统执行LTP测试框架用例
下一篇:
Pytest----parametrize参数化中使用xfail标记
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件