参数化parametrize
pytest.mark.parametrize 装饰器可以实现测试用例参数化 。
parametrize里面的参数a,b,c要和 测试用例test_01 里面的 a,b,c 要一样 。
如果测试输入的参数有多个时候, 列表和元组都可以存放数据 。
多参数组合时,从最近的开始调用 。
示例代码如下:
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/16 17:21 import pytest def sum(c, d): return c + d # parametrize里面的参数a,b,c要和 测试用例test_01 里面的 a,b,s 要一样。 @pytest.mark.parametrize('a, b, s', [(1, 2, 3), (1, 2, 4)]) def test_01(a, b, s): assert sum(a, b) == s
如果测试输入的参数有多个时候, 列表和元组、字典 都可以存放数据。
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/16 17:21 import pytest def login(username, paswd): print(username) print(paswd) return 'sucess' @pytest.mark.parametrize('username, paswd, excepted', [('sky', '123', 'sucess'), ('jack', '456', 'fail')] ) def test_01(username, paswd, excepted): result = login(username, paswd) assert result == excepted @pytest.mark.parametrize('username, paswd, excepted', [['sky', '123', 'sucess'], ['jack', '456', 'fail']] ) def test_02(username, paswd, excepted): result = login(username, paswd) assert result == excepted
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>pytest -s .\test_02.py =================================================================================== test session starts ==================================================================================== platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0 collected 4 items test_02.py sky 123 .jack 456 Fsky 123 .jack 456 F ========================================================================================= FAILURES ========================================================================================= __________________________________________________________________________________ test_01[jack-456-fail] __________________________________________________________________________________ username = 'jack', paswd = '456', excepted = 'fail' @pytest.mark.parametrize('username, paswd, excepted', [('sky', '123', 'sucess'), ('jack', '456', 'fail')] ) def test_01(username, paswd, excepted): result = login(username, paswd) > assert result == excepted E AssertionError: assert 'sucess' == 'fail' E - fail E + sucess test_02.py:31: AssertionError __________________________________________________________________________________ test_02[jack-456-fail] __________________________________________________________________________________ username = 'jack', paswd = '456', excepted = 'fail' @pytest.mark.parametrize('username, paswd, excepted', [['sky', '123', 'sucess'], ['jack', '456', 'fail']] ) def test_02(username, paswd, excepted): result = login(username, paswd) > assert result == excepted E AssertionError: assert 'sucess' == 'fail' E - fail E + sucess test_02.py:43: AssertionError ================================================================================= short test summary info ================================================================================== FAILED test_02.py::test_01[jack-456-fail] - AssertionError: assert 'sucess' == 'fail' FAILED test_02.py::test_02[jack-456-fail] - AssertionError: assert 'sucess' == 'fail' =============================================================================== 2 failed, 2 passed in 0.20s ================================================================================ D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>
输入参数很多时,可以放到 testinput 字典
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/16 17:21 import pytest # 用户名,密码,验证码 def login(username, paswd, veri_code): print(username) print(paswd) print(veri_code) return 'sucess' @pytest.mark.parametrize('test_input, excepted', [[{'username' : 'sky', 'paswd' : '123', 'veri_code' : '123'}, 'sucess'], [{'username' : 'jack', 'paswd' : '123', 'veri_code' : '123'}, 'fail']] ) def test_02(test_input, excepted): result = login(test_input['username'], test_input['paswd'], test_input['veri_code']) assert result == excepted
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>pytest -s .\test_02.py =================================================================================== test session starts ==================================================================================== platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0 collected 2 items test_02.py sky 123 123 .jack 123 123 F ========================================================================================= FAILURES ========================================================================================= ________________________________________________________________________________ test_02[test_input1-fail] _________________________________________________________________________________ test_input = {'paswd': '123', 'username': 'jack', 'veri_code': '123'}, excepted = 'fail' @pytest.mark.parametrize('test_input, excepted', [[{'username' : 'sky', 'paswd' : '123', 'veri_code' : '123'}, 'sucess'], [{'username' : 'jack', 'paswd' : '123', 'veri_code' : '123'}, 'fail']] ) def test_02(test_input, excepted): result = login(test_input['username'], test_input['paswd'], test_input['veri_code']) > assert result == excepted E AssertionError: assert 'sucess' == 'fail' E - fail E + sucess test_02.py:22: AssertionError ================================================================================= short test summary info ================================================================================== FAILED test_02.py::test_02[test_input1-fail] - AssertionError: assert 'sucess' == 'fail' =============================================================================== 1 failed, 1 passed in 0.18s ================================================================================ D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>
参数组合的情况
# 先调用靠最近的参数y,然后再调用x @pytest.mark.parametrize('x', [1, 2]) @pytest.mark.parametrize('y', [3, 4])
若要获得多个参数组合的情况,笛卡尔积
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/16 17:21 import pytest # 先调用靠最近的参数y,然后再调用x @pytest.mark.parametrize('x', [1, 2]) @pytest.mark.parametrize('y', [3, 4]) def test_01(x, y): print('\n测试数据的组合:y——>{0}, x——>{1}'.format(x, y))
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>pytest -s .\test_02.py =================================================================================== test session starts ==================================================================================== platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0 collected 4 items test_02.py 测试数据的组合:y——>1, x——>3 . 测试数据的组合:y——>2, x——>3 . 测试数据的组合:y——>1, x——>4 . 测试数据的组合:y——>2, x——>4 . ==================================================================================== 4 passed in 0.03s ===================================================================================== D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>