pytest实现测试用例参数化
作者:阿刁阿 发布时间:2023-12-10 19:01:21
标签:pytest,测试,用例,参数化
背景
本文总结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


猜你喜欢
- Python安装过程,供大家参考,具体内容如下1.下载安装程序我们安装Python的一个重要目的是为了用IAR编译CC2640 OAD文件时
- 进入root 权限下apt-get install mysql-serverapt-get install mysql-client创建数据
- mysql_query("set autocommit=0"); $list_one = $db->fetch_f
- 一、MySQL删除外键格式:alter table 表名 drop foreign key 外键名;表名就是有外键存在的那个表。外键名可以通
- 目录图像翻转图像轮廓排序图像轮廓排序颜色识别基础颜色识别根据BGR获取HSV阈值编辑器图像翻转使用Python的一个包,imutils。使用
- 本文实例为大家分享了js判断密码强度的具体代码,供大家参考,具体内容如下<!DOCTYPE html><html>&
- 1. 概念显著性检测,就是使用图像处理技术和计算机视觉算法来定位图片中最“显著”的区域。显著区域就是
- Pytorch的核心是两个主要特征:1.一个n维tensor,类似于numpy,但是tensor可以在GPU上运行2.搭建和训练神经网络时的
- 前言.net core来势已不可阻挡。既然挡不了,那我们就顺应它。了解它并学习它。今天我们就来看看和之前.net版本的配置文件读取方式有何异
- http://www.gotapi.com/ 语言:英语 简介:HTML,CSS,XPATH,XSL,JAVASCRIP
- 首先获取ip:<% userip=Request.ServerVariables(&qu
- 什么是 docopt?1、docopt 是一种 Python 编写的命令行执行脚本的交互语言。它是一种语言!它是一种语言!它是一种语言!2、
- 数据处理版本1#数据处理import osimport torchfrom torch.utils import datafrom PIL
- 前言我的JavaScript水平比较一般.好吧,是相当的一般.因此,对于最新的前端框架技术,实在是有点困难,但现实让我必须面对.因此,学习是
- 最近在做一个项目,用双通道神经网络,每个通道输入不同数据训练,具有相同label。开始没想到如何实现,网上很多例子都是单通道,即便找到双通道
- 【MySql常用命令】1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql> SHOW DATABASES;2:创建一个数
- 本文实例讲述了Python操作多维数组输出和矩阵运算。分享给大家供大家参考,具体如下:在许多编程语言中(Java,COBOL,BASIC),
- Go 语言 switch 语句switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上直下逐一测试,直到匹配为
- python matplotlib画图使用colorbar工具自定义颜色 colorbar(draw colorbar without an
- 如果你没有序列号,那么就只能在上面的三种free edition(Enterprise Evaluation、Express、Express