pytest入门到放弃8--parametrize参数化函数文档介绍

parametrize 允许在测试函数或类中定义多组参数和fixtures
parametrize 函数文档如下:
def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None):
    """ Add new invocations to the underlying test function using the list
    of argvalues for the given argnames.  Parametrization is performed
    during the collection phase.  If you need to setup expensive resources
    see about setting indirect to do it rather at test setup time.  # 使用给定argnames的argValue列表向基础测试函数添加新的调用。在收集阶段执行参数化。

    :arg argnames: a comma-separated string denoting one or more argument
                   names, or a list/tuple of argument strings.  # 参数名:使用用逗号分隔的字符串,或列表或元祖,表示一个或多个参数名

    :arg argvalues: The list of argvalues determines how often a
        test is invoked with different argument values.  If only one
        argname was specified argvalues is a list of values.  If N
        argnames were specified, argvalues must be a list of N-tuples,
        where each tuple-element specifies a value for its respective
        argname.  # 参数值:只有一个argnames,argvalues则是值列表。有N个argnames时,每个元祖对应一组argnames,所有元祖组合成一个列表

    :arg indirect: The list of argnames or boolean. A list of arguments'
        names (self,subset of argnames). If True the list contains all names from
        the argnames. Each argvalue corresponding to an argname in this list will
        be passed as request.param to its respective argname fixture
        function so that it can perform more expensive setups during the
        setup phase of a test rather than at collection time.  # indirect:当indirect=True时,若传入的argnames是fixture函数名,此时fixture函数名将成为一个可执行的函数,
                                        argvalues作为fixture的参数,执行fixture函数,最终结果再存入 request.param;
                                        当indirect=False时,fixture函数只作为一个参数名给测试收集阶段调用。
                                       # 什么是 the setup phase 测试设置阶段?    理解为配置 conftest.py 阶段
                                       # 什么是 the collection phase 测试收集阶段? 理解为 用例执行 阶段
:arg ids: list of string ids, or a callable. If strings, each is corresponding to the argvalues so that they are part of the test id. If None is given as id of specific test, the automatically generated id for that argument will be used. If callable, it should take one argument (self,a single argvalue) and return a string or return None. If None, the automatically generated id for that argument will be used. If no ids are provided they will be generated automatically from the argvalues.  # ids:字符串列表,可以理解成标题,与用例个数保持一致 :arg scope: if specified it denotes the scope of the parameters. The scope is used for grouping tests by parameter instances. It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration.  # 如果指定,则表示参数的范围。作用域用于按参数实例对测试进行分组。它还将覆盖任何fixture函数定义的范围,允许使用测试上下文或配置设置动态范围。
"""

 

使用parametrize: 

1)以除法为例(ids 用例标题,与 argvalues 列表长度一致):

# File  : test_demo_10.py
# IDE   : PyCharm

import pytest

def division(a, b):
    return int(a / b)

@pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
def test_1(a, b, c):
    res = division(a, b)
    assert res == c
# File  : test_demo_10.py
# IDE   : PyCharm

import pytest

def division(a, b):
    return int(a / b)

@pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
def test_1(a, b, c):
    res = division(a, b)
    assert res == c

执行结果:

E:\personal\python38\python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
test_demo_10.py::test_1[整除]
test_demo_10.py::test_1[被除数为0]
test_demo_10.py::test_1[除数为0]
test_demo_10.py::test_1[非整除]
..F.
================================== FAILURES ===================================
________________________________ test_1[除数为0] _________________________________

a = 1, b = 0, c = 0

    @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
    def test_1(a, b, c):
>       res = division(a, b)

test_demo_10.py:15: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

a = 1, b = 0

    def division(a, b):
>       return int(a / b)
E       ZeroDivisionError: division by zero

test_demo_10.py:11: ZeroDivisionError
=========================== short test summary info ===========================
FAILED test_demo_10.py::test_1[除数为0] - ZeroDivisionError: division by zero
1 failed, 3 passed in 0.10s

Process finished with exit code 0

 

2)parametrize 还可以叠加使用(顺便看看叠加后,都加上 ids 会有什么效果):

# File  : test_demo_10.py
# IDE   : PyCharm

import pytest

def division(a, b):
    return int(a / b)

@pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
def test_1(a, b, c):
    res = division(a, b)
    assert res == c

@pytest.mark.parametrize('a', [100], ids=['1叠加parametrize'])
@pytest.mark.parametrize('b, c', [(4,25)], ids=['2叠加parametrize'])
def test_1(a, b, c):
    res = division(a, b)
    assert res == c

执行代码:

  1)叠加后 ids 也会叠加

  2)叠加后,执行顺序是从上玩下依次执行

test_demo_10.py::test_1[2叠加parametrize-1叠加parametrize]
.
1 passed in 0.07s

 

你可能感兴趣的:(pytest入门到放弃8--parametrize参数化函数文档介绍)