pytest之什么是 parametrize参数化?

我们都知道pytest和unittest是兼容的,但是它也有不兼容的地方,比如ddt数据驱动,测试夹具fixtures(即setup、teardown)这些功能在pytest中都不能使用了,因为pytest已经不再继承unittest了。不使用ddt数据驱动那pytest是如何实现参数化的呢?答案就是mark里自带的一个参数化标签
摘要由CSDN通过智能技术生成

前言

我们都知道pytest和unittest是兼容的,但是它也有不兼容的地方,比如ddt数据驱动,测试夹具fixtures(即setup、teardown)这些功能在pytest中都不能使用了,因为pytest已经不再继承unittest了。

不使用ddt数据驱动那pytest是如何实现参数化的呢?答案就是mark里自带的一个参数化标签。

一、源码解读

关键代码:@pytest.mark.parametrize

​ 我们先看下源码:def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None):,按住ctrl然后点击对应的函数名就可查看源码。

    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.

        :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.

        :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.

        :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.

        :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.
        """

​ 我们来看下主要的四个参数:

​ 参数1-argnames:一个或多个参数名,用逗号分隔的字符串,如"arg1,arg2,arg3",或参数字符串的列表/元组。需要注意的是,参数名需要与用例的入参一致。

​ 参数2-argvalues:参数值,必须是列表类型;如果有多个参数,则用元组存放值,一个元组存放一组参数值,元组放在列表。(实际上元组包含列表、列表包含列表也是可以的,可以动手试一下)

# 只有一个参数username时,列表里都是这个参数的值:
@pytest.mark.parametrize("username", ["user1", "user2", "user3"])
# 有多个参数username、pwd,用元组存放参数值,一个元组对应一组参
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
`@pytest.fixture`和`@pytest.mark.parametrize`是pytest中的两个常用装饰器,用于测试用例的编写和组织。 `@pytest.fixture`装饰器用于定义测试用例执行前需要进行的准备工作,也就是测试夹具(test fixture)。夹具可以是创建、配置或者初始测试用例所需的对象、数据或环境等。通过夹具,我们可以确保在每个测试用例执行前都有一致的环境和数据状态。 举个例子,假设我们有一个测试用例需要访问数据库,那么可以使用`@pytest.fixture`装饰器来创建一个数据库连接夹具,在每个测试用例执行前都创建一个连接,在用例执行后关闭连接,以保证每个测试用例都有独立的数据库连接。 `@pytest.mark.parametrize`装饰器用于参数测试用例。参数是一种有效的测试用例设计方法,它允许我们通过给定不同的参数组合来运行相同的测试逻辑,从而减少代码重复并增加测试覆盖性。使用`@pytest.mark.parametrize`装饰器,我们可以将测试用例定义为一个参数的函数,并指定不同的参数组合。 举个例子,假设我们有一个加法函数`add(a, b)`,可以使用`@pytest.mark.parametrize`装饰器来定义一个参数测试用例,传入不同的参数组合来验证加法函数的正确性,例如测试用例可以是`(1, 2, 3)`、`(0, 0, 0)`、`(-1, 1, 0)`等。 总结来说,`@pytest.fixture`用于准备测试用例执行前的环境和数据,而`@pytest.mark.parametrize`用于参数测试用例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值