pytest实现测试用例参数化
本文介绍如何利用pytest的@pytest.mark.parametrize装饰器实现测试用例参数化。通过单参数、多参数及嵌套参数化的示例,展示如何高效覆盖多组测试数据,特别是多条件组合场景下的全量用例生成,提升自动化测试效率。
背景
本文总结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个查询条件且每个条件有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)装饰器。
到此这篇关于pytest实现测试用例参数化的文章就介绍到这了,更多相关pytest 测试用例参数化内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













