pytest之parametrize参数化

本文介绍了pytest的parametrize参数化功能,包括源码解读、单个和多个参数化用法、标记数据以及用例ID的设定。通过示例展示了如何使用parametrize进行测试用例的创建,并提到了与unittest的ddt数据驱动的区别。文章末尾提供了相关项目实战资源和福利。
摘要由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,用元组存放参数值,一个元组对应一组参数:
@pytest.mark.parametrize("username, pwd", [("user1", "pwd1"), ("user2", "pwd2"), ("use
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值