pytest实现测试用例参数化
作者:阿刁阿 时间:2023-12-10 19:01:21
背景
本文总结pytest的测试用例参数化。
说明
软件测试中,输入相应值,检查期望值,是常见测试方法。
在自动化测试中,一个测试用例对应一个测试点,通常一组测试数据无法完全覆盖测试范围,所以,需要参数化来传递多组数据。
pytest的测试用例参数化使用如下装饰器即可完成。
@pytest.mark.parametrize(argnames, argvalues)
# 参数:
# argnames:以逗号分隔的字符串
# argvaluse: 参数值列表,若有多个参数,一组参数以元组形式存在,包含多组参数的所有参数
# 以元组列表形式存在
示例:
参数化之一个参数。
# ./test_case/test_func.py
import pytest
@pytest.mark.parametrize("arg_1", [4399, 2012])
def test_add_by_func_aaa(arg_1):
print(arg_1)
# ./run_test.py
import pytest
if __name__ == '__main__':
pytest.main(['-v','-s'])
'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items
test_case/test_func.py::test_add_by_func_aaa[4399] 4399
PASSED
test_case/test_func.py::test_add_by_func_aaa[2012] 2012
PASSED
============================== 2 passed in 0.04s ==============================
[Finished in 1.3s]
'''
参数化之多个参数。
# ./test_case/test_func.py
import pytest
@pytest.mark.parametrize("arg_1, arg_2", [(4399, 'AAAA'), (2012, 'BBBB')])
def test_add_by_func_aaa(arg_1,arg_2):
print("arg_1:{} arg_2:{}".format(arg_1, arg_2))
# ./run_test.py
import pytest
if __name__ == '__main__':
pytest.main(['-v','-s'])
'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items
test_case/test_func.py::test_add_by_func_aaa[4399-AAAA] arg_1:4399 arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[2012-BBBB] arg_1:2012 arg_2:BBBB
PASSED
============================== 2 passed in 0.05s ==============================
[Finished in 1.3s]
'''
以上第2个示例,展现的是一个测试用例有两个参数,然后参数化了两组数据。
但在实际测试中,有的场景,比如多条件查询,比如有2个查询条件,每个条件有3个选项,如果要全部覆盖,则是3*3==9种情况。这种情景,人工测试一般是不会全部覆盖的,但在自动化测试中,只要你想,就可以做到。如下示例:
如下格式参数化,其测试结果为所有参数选项数量的乘积。
# ./test_case/test_func.py
import pytest
from func import *
'''
class TestFunc:
# 正常测试用例
def test_add_by_class(self):
assert add(2,3) == 5
def test_add_by_class_11(self):
assert add(2,3) == 5
'''
@pytest.mark.parametrize("arg_1", [4399, 2012, 1997])
@pytest.mark.parametrize("arg_2", ['AAAA', 'BBBB', 'CCCC'])
def test_add_by_func_aaa(arg_1,arg_2):
print("arg_1:{} arg_2:{}".format(arg_1, arg_2))
# ./run_test.py
import pytest
if __name__ == '__main__':
pytest.main(['-v','-s'])
'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 9 items
test_case/test_func.py::test_add_by_func_aaa[AAAA-4399] arg_1:4399 arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[AAAA-2012] arg_1:2012 arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[AAAA-1997] arg_1:1997 arg_2:AAAA
PASSED
test_case/test_func.py::test_add_by_func_aaa[BBBB-4399] arg_1:4399 arg_2:BBBB
PASSED
test_case/test_func.py::test_add_by_func_aaa[BBBB-2012] arg_1:2012 arg_2:BBBB
PASSED
test_case/test_func.py::test_add_by_func_aaa[BBBB-1997] arg_1:1997 arg_2:BBBB
PASSED
test_case/test_func.py::test_add_by_func_aaa[CCCC-4399] arg_1:4399 arg_2:CCCC
PASSED
test_case/test_func.py::test_add_by_func_aaa[CCCC-2012] arg_1:2012 arg_2:CCCC
PASSED
test_case/test_func.py::test_add_by_func_aaa[CCCC-1997] arg_1:1997 arg_2:CCCC
PASSED
============================== 9 passed in 0.06s ==============================
[Finished in 1.4s]
'''
总结
以上,就是我们测试中使用的pytest测试用例参数化。
当然,如实际需要,你也可以把测试数据独立到文件里。然后读取出来,传递给@pytest.mark.parametrize(argnames, argvalues)装饰器
来源:https://blog.csdn.net/lc_buzhidao/article/details/105206216
标签:pytest,测试,用例,参数化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go 语言结构体链表的基本操作
2024-02-07 18:51:11
![](https://img.aspxhome.com/file/2023/4/101044_0s.png)
pyecharts如何实现显示数据为百分比的柱状图
2021-06-27 17:11:52
![](https://img.aspxhome.com/file/2023/4/72134_0s.png)
tensorflow 模型权重导出实例
2022-10-14 06:25:25
推荐20家国外的脚本下载网站
2023-08-23 11:36:02
![](https://img.aspxhome.com/file/2023/2/55992_0s.jpg)
pyqt4教程之messagebox使用示例分享
2023-11-06 08:09:03
关于python并发编程中的协程
2023-10-18 04:37:44
超级链接中MailTo的语法
2008-08-29 13:00:00
Python第三方包PrettyTable安装及用法解析
2023-02-22 03:19:16
![](https://img.aspxhome.com/file/2023/8/107218_0s.png)
web.py获取上传文件名的正确方法
2021-01-22 16:00:06
VSCode + WSL 2 + Ruby环境搭建图文详解
2022-09-10 21:05:38
![](https://img.aspxhome.com/file/2023/0/127240_0s.gif)
Python操作CSV格式文件的方法大全
2023-07-08 18:57:45
![](https://img.aspxhome.com/file/2023/8/71488_0s.png)
pytorch中的inference使用实例
2023-07-24 16:22:56
MySQL 8 新特性之Invisible Indexes
2024-01-19 04:56:06
explain命令为什么可能会修改MySQL数据
2024-01-19 14:53:49
Python编解码问题及文本文件处理方法详解
2021-04-13 07:52:06
![](https://img.aspxhome.com/file/2023/9/72999_0s.png)
MySQL中实现高性能高并发计数器方案(例如文章点击数)
2024-01-19 00:43:09
Django框架基础认证模块auth应用示例
2023-11-10 07:48:51
![](https://img.aspxhome.com/file/2023/0/64240_0s.png)
js style动态设置table高度
2024-02-23 23:23:05
mysql 索引合并的使用
2024-01-14 05:53:39
mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
2023-11-18 06:10:15