软件测试中,输入相应值,检查期望值,是常见测试方法。在自动化测试中,一个测试用例对应一个测试点,通常一组测试数据无法完全覆盖测试范围,所以,需要参数化来传递多组数据。
pytest的测试用例参数化使用如下装饰器即可完成
@pytest.mark.parametrize(argnames, argvalues)
主要参数说明
【argsnames :参数名】是个字符串,如中间用逗号分隔则表示为多个参数名
【argsvalues :参数值】参数组成的列表,列表中有几个元素,就会生成几条用例
使用方法使用
@pytest.mark.paramtrize() 装饰测试方法
parametrize("data", param) 中的 “data” 是自定义的参数名,param 是引入的参数列表,将自定义的参数名 data 作为参数传给测试用例 test_func,然后就可以在测试用例内部使用 data 的参数了
data = user ;param = "小明"、"大熊"
@pytest.mark.parametrize("user", ["小明", "大熊"])
单个参数实例
# coding=utf-8
# authou:shichao
import pytest
@pytest.mark.parametrize('user', ['shichao', 'xiaoming'])
def test_user(user):
print(user)
if __name__ == '__main__':
pytest.main('-v', '-s')
控制台输出结果
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- F:\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: F:\Python\Python38-32\python_code\Study\Python_Pytest\test_parametrize
collecting ... collected 2 items
test_parametrize01.py::test_user[shichao] PASSED [ 50%]shichao
test_parametrize01.py::test_user[xiaoming] PASSED [100%]xiaoming
============================== 2 passed in 0.02s ==============================
Process finished with exit code 0
多个参数实例
# coding=utf-8
# authou:shichao
import pytest
@pytest.mark.parametrize('user,passwd', [('shichao','123456'), ('xiaoming','test123')])
def test_user(user,passwd):
print('user:{},passwd:{}'.format(user,passwd))
if __name__ == '__main__':
pytest.main('-v', '-s')
控制台输入的结果
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- F:\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: F:\Python\Python38-32\python_code\Study\Python_Pytest\test_parametrize
collecting ... collected 2 items
test_parametrize01.py::test_user[shichao-123456] PASSED [ 50%]user:shichao,passwd:123456
test_parametrize01.py::test_user[xiaoming-test123] PASSED [100%]user:xiaoming,passwd:test123
============================== 2 passed in 0.02s ==============================
Process finished with exit code 0
以上多个参数示例,展现的是一个测试用例有两个user、passwd俩个参数,然后进行参数化了两组数据
但在实际测试中,很多场景涉到多组覆盖,比如搜索多条件查询,比如有3个查询条件,每个条件有4个选项,如果要全部覆盖,则是8*8==64种情况
这种情景,人工测试一般很难全部覆盖的,但在自动化测试中,只要你想,就可以做到。如下示例:如下格式参数化,其测试结果为所有参数选项数量的乘积
# coding=utf-8
# authou:shichao
import pytest
@pytest.mark.parametrize('user', ['shichao','xiaoming','XH','XD'])
@pytest.mark.parametrize('type01',['完成','未完成',"待处理","已过期"])
@pytest.mark.parametrize('data02',['20201223','20201224','20201225','20201226'])
def test_user(user,type01,data02):
print('user:{},type01:{},data02:{}'.format(user,type01,data02))
if __name__ == '__main__':
pytest.main('-v', '-s')
控制台输出的结果
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- F:\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: F:\Python\Python38-32\python_code\Study\Python_Pytest\test_parametrize
collecting ... collected 64 items
test_parametrize01.py::test_user[20201223-\u5b8c\u6210-shichao] PASSED [ 1%]user:shichao,type01:完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u5b8c\u6210-xiaoming] PASSED [ 3%]user:xiaoming,type01:完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u5b8c\u6210-XH] PASSED [ 4%]user:XH,type01:完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u5b8c\u6210-XD] PASSED [ 6%]user:XD,type01:完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u672a\u5b8c\u6210-shichao] PASSED [ 7%]user:shichao,type01:未完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u672a\u5b8c\u6210-xiaoming] PASSED [ 9%]user:xiaoming,type01:未完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u672a\u5b8c\u6210-XH] PASSED [ 10%]user:XH,type01:未完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u672a\u5b8c\u6210-XD] PASSED [ 12%]user:XD,type01:未完成,data02:20201223
test_parametrize01.py::test_user[20201223-\u5f85\u5904\u7406-shichao] PASSED [ 14%]user:shichao,type01:待处理,data02:20201223
test_parametrize01.py::test_user[20201223-\u5f85\u5904\u7406-xiaoming] PASSED [ 15%]user:xiaoming,type01:待处理,data02:20201223
test_parametrize01.py::test_user[20201223-\u5f85\u5904\u7406-XH] PASSED [ 17%]user:XH,type01:待处理,data02:20201223
test_parametrize01.py::test_user[20201223-\u5f85\u5904\u7406-XD] PASSED [ 18%]user:XD,type01:待处理,data02:20201223
test_parametrize01.py::test_user[20201224-\u5f85\u5904\u7406-XH] PASSED [ 42%]user:XH,type01:待处理,data02:20201224
test_parametrize01.py::test_user[20201224-\u5f85\u5904\u7406-XD] PASSED [ 43%]user:XD,type01:待处理,data02:20201224
test_parametrize01.py::test_user[20201224-\u5df2\u8fc7\u671f-shichao] PASSED [ 45%]user:shichao,type01:已过期,data02:20201224
test_parametrize01.py::test_user[20201224-\u5df2\u8fc7\u671f-xiaoming] PASSED [ 46%]user:xiaoming,type01:已过期,data02:20201224
test_parametrize01.py::test_user[20201224-\u5df2\u8fc7\u671f-XH] PASSED [ 48%]user:XH,type01:已过期,data02:20201224
test_parametrize01.py::test_user[20201224-\u5df2\u8fc7\u671f-XD] PASSED [ 50%]user:XD,type01:已过期,data02:20201224
test_parametrize01.py::test_user[20201226-\u5df2\u8fc7\u671f-XH] PASSED [ 98%]user:XH,type01:已过期,data02:20201226
test_parametrize01.py::test_user[20201226-\u5df2\u8fc7\u671f-XD] PASSED [100%]user:XD,type01:已过期,data02:20201226
============================= 64 passed in 0.19s ==============================
Process finished with exit code 0
删减后的结果,太长,理解意思就行
以上几个实例,就是我们测试中使用的pytest测试框架测试用例参数化
当然,如实际需要,你也可以把测试数据独立到文件里,然后读取出来,传递给@pytest.mark.parametrize(argnames, argvalues)装饰器,最后我们前期讲到了Python自动化测试 | Pytest之fixture 可温习