pytest framework -- parameterization (parametrize decorator)

Note:
In software testing, it is a common testing method to input corresponding values ​​and check expected values.
In automated testing, a test case corresponds to a test point, and usually a set of test data cannot fully cover the test range, so parameterization is required to pass multiple sets of data.
pytest's test case parameterization can be done using the following decorator.

Parameterization: parametrize decorator
@pytest.mark.parametrize(argnames, argvalues)
argnames: comma-separated string
argvalues: parameter value list, if there are multiple parameters, a set of parameters exists in tuple form, including multiple sets of parameters All parameters of , exist in the form of a list of tuples

1. Single data

import pytest
list = ['小明', '小花']
@pytest.mark.parametrize('name', list)
def test_f01(name):
    print(f'他的名字是{
      
      name}')
if __name__ == '__main__':
    pytest.main(['test_parametrize.py', '-sv'])

The result is:
insert image description here

2. Multiple data

import pytest
list = [('小明', 20), ('小花', 15)]
@pytest.mark.parametrize('name, age', list)
def test_f01(name, age):
    print(f'{
      
      name}今年{
      
      age}岁了')
if __name__ == '__main__':
    pytest.main(['test_parametrize.py', '-sv'])

The result is:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45422695/article/details/123091183