Python测试框架之pytest高阶用法:函数数据参数化(五)

函数数据参数化

方便测试函数对测试数据的获取。
 方法:
     parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
 常用参数:
     argnames:参数名
     argvalues:参数对应值,类型必须为list
                 当参数为一个时格式:[value]
                 当参数个数大于一个时,格式为:[(param_value1,param_value2.....),(param_value1,param_value2.....)]
 使用方法:
     @pytest.mark.parametrize(argnames,argvalues)
     ️ 参数值为N个,测试方法就会运行N次
  
 

  单个参数示例:

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    @pytest.mark.parametrize("a", [3, 6])  # a参数被赋予两个值,函数会运行两遍
    def test_a(self, a):  # 参数必须和parametrize里面的参数一致
        print("test data:a=%d" % a)
        assert a % 3 == 0

  

 

多个参数示例:

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    @pytest.mark.parametrize("a,b", [(1, 2), (0, 3)])  # 参数a,b均被赋予两个值,函数会运行两遍
    def test_a(self, a, b):  # 参数必须和parametrize里面的参数一致
        print("test data:a=%d,b=%d" % (a, b))
        assert a + b == 3

  

 

函数返回值类型示例:

import pytest
def return_test_data():
    return [(1,2),(0,3)]
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
            print("------->teardown_class")

    @pytest.mark.parametrize("a,b", return_test_data())  # 使用函数返回值的形式传入参数值
    def test_a(self, a, b):
        print("test data:a=%d,b=%d" % (a, b))
        assert a + b == 3

  

 附上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中ids的用法: 

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

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
if __name__ == '__main__':
    pytest.main(["-s","test_abc.py"])

  

 使用 pytest.mark.parametrize 参数化的时候,加 ids 参数用例描述有中文时,在控制台输出会显示unicode编码,中文不能正常显示。
使用 pytest_collection_modifyitems 钩子函数,对输出的 item.name 和 item.nodeid 重新编码。

pytest_collection_modifyitems

在项目的根目录写个 conftest.py 文件,加以下代码

def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时,将收集到的item的name和nodeid的中文显示在控制台上
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        print(item.nodeid)
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")

  重新运行,查看结果

 或者在pytest.ini文件里添加下面一行代码:

[pytest]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True

  以上两种方式都好使

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

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,75], ids=['1叠加parametrize','3叠加parametrize'])
@pytest.mark.parametrize('b, c', [(4,25),(3,25)], ids=['2叠加parametrize','4叠加parametrize'])
def test_1(a, b, c):
    res = division(a, b)
    assert res == c

  执行结果

============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: E:\PycharmProjects\lianxi, configfile: pytest.ini
plugins: forked-1.3.0, html-1.22.1, metadata-1.8.0, rerunfailures-9.1, xdist-2.1.0
test_abc.py::test_1[2\u53e0\u52a0parametrize-1\u53e0\u52a0parametrize]
test_abc.py::test_1[2\u53e0\u52a0parametrize-3\u53e0\u52a0parametrize]
test_abc.py::test_1[4\u53e0\u52a0parametrize-1\u53e0\u52a0parametrize]
test_abc.py::test_1[4\u53e0\u52a0parametrize-3\u53e0\u52a0parametrize]
collected 4 items

test_abc.py .FF.

================================== FAILURES ===================================
____________________ test_1[2叠加parametrize-3叠加parametrize] ____________________

a = 75, b = 4, c = 25

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

test_abc.py:286: AssertionError
____________________ test_1[4叠加parametrize-1叠加parametrize] ____________________

a = 100, b = 3, c = 25

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

test_abc.py:286: AssertionError
------ generated html file: file://E:\PycharmProjects\lianxi\report.html ------
=========================== short test summary info ===========================
FAILED test_abc.py::test_1[2叠加parametrize-3叠加parametrize] - assert 18 == 25
FAILED test_abc.py::test_1[4叠加parametrize-1叠加parametrize] - assert 33 == 25
========================= 2 failed, 2 passed in 0.13s =========================

  由上可得结论

1.叠加使用和不叠加使用同时存在时,优先叠加使用
2.叠加后 ids 也会叠加
3.叠加后,显示顺序是下面的叠加在上面的前面
4.参数执行顺序是(a=100,b=4,c=25),(a=75,b=4,c=25),(a=100,b=3,c=25),(a=75,b=3,c=25)

  

 

posted @ 2020-10-11 23:20  飞鸟与新月  阅读(2135)  评论(0编辑  收藏  举报