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

晒酷学院:https://shareku.ke.qq.com/
微信号:添加请注明晒酷学院            QQ群:979438600
微信号:添加请注明晒酷学院         QQ群:979438600
       
函数数据参数化

方便测试函数对测试数据的获取。
 方法:
     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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
pytest是一个Python测试框架,它是基于`unittest`和`nose`这两个Python测试框架的经验基础上发展而来的。相比于其他测试框架pytest具有更简洁、更灵活的语法,让编写和管理测试用例更加方便。 在使用pytest之前,我们需要将其导入到我们的Python项目中。可以通过`pip`命令来安装pytest包,具体命令为`pip install pytest`。安装完成后,我们可以在Python代码中通过`import pytest`语句来引入pytest模块。 导入pytest后,我们就可以使用pytest提供的丰富的装饰器和断言函数来编写测试用例。pytest支持使用`@pytest.fixture`装饰器定义测试用例前后需要执行的操作,比如创建数据库连接等。使用`@pytest.mark.parametrize`装饰器可以方便地对测试用例进行参数化,减少重复性的代码。 pytest还提供了丰富的断言函数,用于验证测试结果是否符合预期。常用的断言函数有`assert`、`assertEqual`、`assertTrue`等。使用这些断言函数可以方便地检查测试用例的执行结果,并在测试失败时输出详细的错误信息,帮助我们快速定位问题。 除了基本的测试用例编写和断言功能外,pytest还提供了插件机制,可以通过插件扩展pytest的功能。例如,`pytest-html`插件可以生成HTML格式的测试报告,`pytest-xdist`插件可以实现用例的并行执行,提高测试效率。 总结来说,pytest是一个功能强大、灵活且易于使用的Python测试框架。通过导入pytest模块,并利用它提供的装饰器和断言函数,我们可以高效地编写和管理测试用例。同时,借助插件机制,我们可以扩展pytest的功能,满足更多的测试需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值