pytest中的fixture参数解析以及用法

当我们在使用pytest实现前后置调用时有两种方式

方式一:

        采用setup/teardown以及setup_class/teardown_class类似这种方式去实现前后置调用

方式二:

        采用pytest中强大的fixture装饰器来实现

本期文章主要采用方式二来解决测试用例前后置调用的问题

首先我们来看下,pytest中的fixture的参数有哪些,分别是scope,params,autouse,ids,name这5个形参,哪些他们分别的作用域以及作用是什么呢? 

下面来说下scope:

“”“
scope:
    function(默认)
    class
    module
    session
params:
    参数化,支持list,tuple,字典列表,字典元组
autouse:
    false(默认)
    True
ids:
    当使用params参数化时,给每一个值设置一个变量名,其实意义不大
name:
    表示的是被@pytest.fixture标记的方法取一个别名,注意:当去了别名之后,原来的名称无法使用
”“”
@pytest.fixture(scope='function')
def my_fixture():
    print('这是前置内容')
    yield
    print('这是后置内容')


class Testdemo():
    
    def test01(self,my_fixture):
        print('这是test01')

    def test02(self):
        print('这是test02')

根据上面的结构,运行后输出的内容为
这是前置内容
这是test01
这是后置内容
这是前置内容
这是test02
这是后置内容

对于上述方法中传入my_fixture的做法,主要是对比setup/teardown之类的更方便,不需要每个方法都在运行之前执行一边setup,而是在某个方法需要这种操作时在执行

下面来说下autouse:

这个参数的使用,主要是弥补如果想使用scope,必须传入my_fixture的缺陷,当引入autouse时,默认是false,我们将false更新为True,此时被fixture装饰的方法将会自动传入每个测试用例方法中,不需要自己传入my_fixture

import  pytest


@pytest.fixture(scope='function',autouse=True)
def my_fixture():
    print('这是前置内容')
    yield
    print('这是后置内容')

class Testdemo():

    def test_01(self):
        print('这是test01')

    def test_02(self):
        print('这是test02')

输出结果为:
testcase/demo1/test_func3.py::Testdemo::test_01 这是前置内容
这是test01
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_02 这是前置内容
这是test02
PASSED这是后置内容

下面来说下params:

主要作用是参数化

import  pytest

@pytest.fixture(scope='function',params=['tom1','tom2'],autouse=True)
def my_fixture(request):
    print('这是前置内容')
    yield request.param
    print('这是后置内容')

class Testdemo():

    def test_01(self,my_fixture):
        print('这是test01',my_fixture)

    def test_02(self):
        print('这是test02')

输出结果:
testcase/demo1/test_func3.py::Testdemo::test_01[tom1] 这是前置内容
这是test01 tom1
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_01[tom2] 这是前置内容
这是test01 tom2
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_02[tom1] 这是前置内容
这是test02
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_02[tom2] 这是前置内容
这是test02
PASSED这是后置内容

这里需要强调的是:

1:params中传入几个值,对应的测试用例就会运行几次,比如传入了两个值tom1和tom2,那么对应测试用例test01和test02就会分别运行两次,这个可以从结果中看到 

 2:params中传入的值是可以获取到的,通过request.param来获取,并返回给用例中传入的my_fixture,我们从结果中的输出就能看到test01比test02多输出了params的值

下面来说下ids:

主要作用是给params中每一个值设置一个变量名,如果params传入的是中文,这个ids可能会用到,其余情况很少会用,因为如果params传入的是中文,那么request.param返回的可能会有编码

import  pytest


@pytest.fixture(scope='function',params=['汤姆1','汤姆2'],autouse=True,ids=['tom1','tom2'])
def my_fixture(request):
    print('这是前置内容')
    yield request.param
    print('这是后置内容')

class Testdemo():

    def test_01(self,my_fixture):
        print('这是test01',my_fixture)

    def test_02(self):
        print('这是test02')

输出的结果:
testcase/demo1/test_func3.py::Testdemo::test_01[tom1] 这是前置内容
这是test01 汤姆1
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_01[tom2] 这是前置内容
这是test01 汤姆2
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_02[tom1] 这是前置内容
这是test02
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_02[tom2] 这是前置内容
这是test02
PASSED这是后置内容


上面所说的乱码会出现在每个用例名称后面的【】中,加了ids就会在【】中显示对应新的变量名

下面来说下name:

主要是给被fixture装饰的方法取个别名,后面测试用例使用时,传别名就行,原来的名称是无效的

import  pytest


@pytest.fixture(scope='function',params=['汤姆1','汤姆2'],autouse=False,ids=['tom1','tom2'],name='demo')
def my_fixture(request):
    print('这是前置内容')
    yield request.param
    print('这是后置内容')

class Testdemo():

    def test_01(self):
        print('这是test01')

    def test_02(self,demo):
        print('这是test02',demo)

输出结果:
testcase/demo1/test_func3.py::Testdemo::test_01 这是test01
PASSED
testcase/demo1/test_func3.py::Testdemo::test_02[tom1] 这是前置内容
这是test02 汤姆1
PASSED这是后置内容

testcase/demo1/test_func3.py::Testdemo::test_02[tom2] 这是前置内容
这是test02 汤姆2
PASSED这是后置内容

从上面可以看出,我传入的被fixture装饰的函数不是my_fixture,而是别名demo 

 

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用和提到了`@pytest.fixture()`的使用方法和参数。`@pytest.fixture()`是pytest测试框架用来标记一个方法作为fixture的装饰器。fixture是pytest用来提供测试所需的资源或配置的方法。 在引用,`@pytest.fixture(scope='function',autouse=True)`的作用是将被装饰的方法作为前置和后置操作,在每个测试用例方法执行前后都会执行该方法。`scope`参数指定了fixture的作用范围,这里设置为`'function'`表示作用于每个测试用例方法。`autouse`参数设为`True`表示自动使用该fixture,不需要在测试用例方法手动传入。 在引用,`@pytest.fixture(scope='function',params=['汤姆1','汤姆2'],autouse=False,ids=['tom1','tom2'],name='demo')`的作用是将被装饰的方法作为前置和后置操作,并且可以通过参数化的方式多次运行测试用例。`params`参数指定了多组参数值,会根据这些参数值运行多次测试。`ids`参数可设置每组参数值的别名,`name`参数fixture指定一个名称。 在引用,`@pytest.fixture(scope='function',params=['tom1','tom2'],autouse=True)`的作用同样是将被装饰的方法作为前置和后置操作,但是这里没有设置别名。这种方式的fixture会自动传给测试用例方法作为参数,测试用例方法需要在参数列表声明。每个参数值会生成一个对应的测试用例。 综上所述,`@pytest.fixture()`是pytest测试框架用来定义fixture的装饰器,通过不同的参数设置可以实现不同的前置和后置操作,并且可以通过参数化的方式多次运行测试用例。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [pytestfixture参数解析以及用法](https://blog.csdn.net/qq_29053519/article/details/125910301)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值