pytest Realizes Test Case Parameterization

  • 2021-10-27 08:11:01
  • OfStack

Background

This paper summarizes the test case parameterization of pytest.

Description

In software testing, it is a common testing method to input corresponding values and check expected values.
In automated testing, one test case corresponds to one test point. Usually, one set of test data can not completely cover the test range, so parameterization is needed to transfer multiple sets of data.

Test case parameterization for pytest can be accomplished using the following decorator.


@pytest.mark.parametrize(argnames, argvalues)
#  Parameters: 
# argnames: Comma-separated strings 
# argvaluse:  Parameter value list, if there are multiple parameters, 1 Group parameters exist as tuples and contain all parameters of multiple groups of parameters 
#  Exists as a tuple list 

Example:

1 parameter of parameterization.


# ./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]
''' 

Parameterized multiple parameters.


# ./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]
'''  

The second example above shows that a test case has two parameters, and then two sets of data are parameterized.

However, in the actual test, there are some scenarios, such as multi-condition query, such as two query conditions, each condition has three options, and if all of them are to be covered, there are 3*3==9 cases. In this scenario, manual testing will not cover all of them, but in automated testing, you can do it if you want. The following example:

The following format is parameterized, and its test result is the product of the number of all parameter options.


# ./test_case/test_func.py
import pytest
from func import *

'''
class TestFunc:

 #  Normal test cases 
 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]
'''

Summarize

The above is the pytest test case parameterization used in our test.

Of course, if necessary, you can also separate the test data into files. Then read it out and pass it to the @ pytest. mark. parametrize (argnames, argvalues) decorator


Related articles: