一、背景
python语言中request+pytest可以灵活的进行API的自动化测试,在自动化过程中,一个测试用例对应一个测试点,通常一组数据无法完全覆盖测试范围,所以,需要参数化来传递多组数据
二、单个参数的参数化
pytest的测试用例参数化使用@pytest.mark.parametrize(argnames, argvalues)装饰器来完成,详细信息如下:
1 @pytest.mark.parametrize(argnames, argvalues) 2 # 参数: 3 # argnames:以逗号分隔的字符串 4 # argvaluse: 参数值列表,若有多个参数,一组参数以元组形式存在,包含多组参数的所有参数 5 # 以元组列表形式存在
实例
1 # ./test_case/test_func.py 2 import pytest 3 4 @pytest.mark.parametrize("arg_1", [924, 925]) 5 def test_add_by_func_aaa(arg_1): 6 print(arg_1) 7 8 # ./run_test.py 9 import pytest 10 11 if __name__ == '__main__': 12 pytest.main(['-v','-s']) 13 14 ''' 15 ============================= test session starts ============================= 16 platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe 17 cachedir: .pytest_cache 18 rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini 19 plugins: allure-pytest-2.8.9, rerunfailures-8.0 20 collecting ... collected 2 items 21 22 test_case/test_func.py::test_add_by_func_aaa[924] 924 23 PASSED 24 test_case/test_func.py::test_add_by_func_aaa[925] 925 25 PASSED 26 27 ============================== 2 passed in 0.04s ============================== 28 [Finished in 1.3s] 29 ''' 30
三、多个参数的参数化
较多场景下,接口的请求体参数都不止一个的,所以需要用到多参数的参数化
实例
1 # ./test_case/test_func.py 2 import pytest 3 4 @pytest.mark.parametrize("arg_1, arg_2", [(924, 'AAAA'), (925, 'BBBB')]) 5 def test_add_by_func_aaa(arg_1,arg_2): 6 print("arg_1:{} arg_2:{}".format(arg_1, arg_2)) 7 8 # ./run_test.py 9 import pytest 10 11 if __name__ == '__main__': 12 pytest.main(['-v','-s']) 13 14 ''' 15 ============================= test session starts ============================= 16 platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe 17 cachedir: .pytest_cache 18 rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini 19 plugins: allure-pytest-2.8.9, rerunfailures-8.0 20 collecting ... collected 2 items 21 22 test_case/test_func.py::test_add_by_func_aaa[924-AAAA] arg_1:924 arg_2:AAAA 23 PASSED 24 test_case/test_func.py::test_add_by_func_aaa[925-BBBB] arg_1:925 arg_2:BBBB 25 PASSED 26 27 ============================== 2 passed in 0.05s ============================== 28 [Finished in 1.3s] 29 '''
以上示例,展现的是一个测试用例有两个参数,然后参数化了两组数据。
在接口测试过程中,两种参数两两组合,最终会有2*2==4种场景。在场景比较复杂的情况下,人工测试一般是不会全部覆盖的,但在自动化测试中,很容易就可以做到。
1 # ./test_case/test_func.py 2 import pytest 3 from func import * 4 5 ''' 6 class TestFunc: 7 8 # 正常测试用例 9 def test_add_by_class(self): 10 assert add(2,3) == 5 11 12 13 def test_add_by_class_11(self): 14 assert add(2,3) == 5 15 ''' 16 17 @pytest.mark.parametrize("arg_1", [4399, 2012, 1997]) 18 @pytest.mark.parametrize("arg_2", ['AAAA', 'BBBB', 'CCCC']) 19 def test_add_by_func_aaa(arg_1,arg_2): 20 print("arg_1:{} arg_2:{}".format(arg_1, arg_2)) 21 22 23 # ./run_test.py 24 import pytest 25 26 if __name__ == '__main__': 27 pytest.main(['-v','-s']) 28 29 30 ''' 31 ============================= test session starts ============================= 32 platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe 33 cachedir: .pytest_cache 34 rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini 35 plugins: allure-pytest-2.8.9, rerunfailures-8.0 36 collecting ... collected 9 items 37 38 test_case/test_func.py::test_add_by_func_aaa[AAAA-4399] arg_1:4399 arg_2:AAAA 39 PASSED 40 test_case/test_func.py::test_add_by_func_aaa[AAAA-2012] arg_1:2012 arg_2:AAAA 41 PASSED 42 test_case/test_func.py::test_add_by_func_aaa[AAAA-1997] arg_1:1997 arg_2:AAAA 43 PASSED 44 test_case/test_func.py::test_add_by_func_aaa[BBBB-4399] arg_1:4399 arg_2:BBBB 45 PASSED 46 test_case/test_func.py::test_add_by_func_aaa[BBBB-2012] arg_1:2012 arg_2:BBBB 47 PASSED 48 test_case/test_func.py::test_add_by_func_aaa[BBBB-1997] arg_1:1997 arg_2:BBBB 49 PASSED 50 test_case/test_func.py::test_add_by_func_aaa[CCCC-4399] arg_1:4399 arg_2:CCCC 51 PASSED 52 test_case/test_func.py::test_add_by_func_aaa[CCCC-2012] arg_1:2012 arg_2:CCCC 53 PASSED 54 test_case/test_func.py::test_add_by_func_aaa[CCCC-1997] arg_1:1997 arg_2:CCCC 55 PASSED 56 57 ============================== 9 passed in 0.06s ============================== 58 [Finished in 1.4s] 59 ''' 60
四、总结
以上就是测试中使用的pytest测试用例参数化,可以实现单参数和多参数的参数化过程。如果测试数据比较多,也可以把测试数据独立到文件里,然后读取出来,传递给@pytest.mark.parametrize(argnames, argvalues)装饰器。