pytest框架之fixture

1. 概述

Pytest的fixture功能灵活好用,支持参数设置,便于进行多用例测试,简单便捷,颇有pythonic。
如果要深入学习pytest,必学fixture。

fixture函数的作用:

 

2. 使用

 

  • 完成setup和teardown操作,处理数据库、文件等资源的打开和关闭
  • 完成大部分测试用例需要完成的通用操作,例如login、设置config参数、环境变量等
  • 准备测试数据,将数据提前写入到数据库,或者通过params返回给test用例,等

2.1. 简介

把一个函数定义为Fixture很简单,只能在函数声明之前加上“@pytest.fixture”。其他函数要来调用这个Fixture,只用把它当做一个输入的参数即可 多个fixture方法,可以互相调用,最好不要递归哈(没试过,有兴趣的童鞋可以试试)

2.2. 使用方式

  • 一个test函数可以包含多个fixture参数
  • fixture作用域:fixture可以修饰单个函数,单个或多个类的成员函数,单个类

下面来介绍pytest特有的方式来写用例

 

1、pytest fixture实例1

代码如下

from __future__ import print_function
import pytest
 
@pytest.fixture()
def resource_a():
    print(‘\nresources_a() "setup"‘) 

def test_1_that_needs_resource_a(resource_a):
    print(‘test_1_that_needs_resource_a()‘)
 
def test_2_that_does_not():
    print(‘\ntest_2_that_does_not()‘)
 
def test_3_that_does(resource_a):
    print(‘test_3_that_does()‘)

使用-s -v运行查看详情如下

技术分享

可以看出测试用例继承了用pytest.fixture的函数后,都执行了setup的功能,默认的pytest.fixture是function

2、代码示例2

代码如下

from __future__ import print_function
import pytest
 
@pytest.fixture(scope=‘module‘)
def resource_a_setup(request):
    print(‘\nresources_a_setup()‘)
    def resource_a_teardown():
        print(‘\nresources_a_teardown()‘)
    request.addfinalizer(resource_a_teardown)
 
def test_1_that_needs_resource_a(resource_a_setup):
    print(‘test_1_that_needs_resource_a()‘)
 
def test_2_that_does_not():
    print(‘\ntest_2_that_does_not()‘)
 
def test_3_that_does(resource_a_setup):
    print(‘\ntest_3_that_does()‘)

使用-s -v运行查看详情如下

技术分享

 

pytest 首先寻找test_ 前缀开头的函数,找到test_1_that_needs_resource_a()函数,然后发现该函数需要一个名为resource_a_setup的参数,就会去寻找有@pytest.fixture标记的名称为resource_a_setup的函数,并构造一个fixture object传递给该测试函数,之后执行测试函数。 函数参数化是一种依赖注入的思想。 

3、使用pytest.fixture的几种方法

在写用例时,有几种方法使用pytest.fixture来形成框架,

方法一:


就是上面提到的这种方法,如下

@pytest.fixture()
def before():
    print(‘\nbefore each test‘)
 
def test_1(before):
    print(‘test_1()‘)
 
def test_2(before):
    print(‘test_2()‘)

方法二:使用fixture修饰:@pytest.mark.usefixtures('fixture1','fixture2')标记测试函数或类

 

@pytest.fixture()
def before():
    print(‘\nbefore each test‘)


@pytest.mark.usefixtures("before") 
def test_1():
    print(‘test_1()‘)
 
@pytest.mark.usefixtures("before") 
def test_2():
    print(‘test_2()‘)

标红的就是修饰器

在fixture object对象使用完以后会有一个destroy 过程,如果想要在destroy过程中进行一些特殊的操作,可以给fixture 函数增加一个request对象,然后定义一个包含你想要执行的操作的函数,调用request的addfinalizer(funct_name)方法来把该函数注册到这个fixture object中。 
可以看一个示例:

@pytest.fixture(scope="module")
def smtp(request):
    smtp = smtplib.SMTP("merlinux.eu")
    def fin():
        print ("teardown smtp")
        smtp.close()
    request.addfinalizer(fin)
    return smtp  # provide the fixture value
 

4、指定fixture作用范围

fixture包含一个scope的可选参数,用于控制fixture执行配置和销毁逻辑的频率。
@pytest.fixture()的scope参数有四个值:
scope='function'
函数级别的fixture每个测试函数 只运行一次。配置代码在测试用例运行之前运行,销毁代码在测试用例运行之后执行。function是fixture参数的默认值。
scope='class'
类级别的fixture每个测试类只运行一次,不管测试类中有多少个类方法都可以共享这个fixture
scope='module'
模块级别的fixture每个模块只运行一次,不管模块里有多少个测试函数,类方法或其他fixture都可以共享这个fixture
scope='session'
会话级别的fixture每次会话只运行一次。一次pytest会话中的所有测试函数、方法都可以共享这个fixture

 

 如下一个用module例子

@pytest.fixture(scope=‘module‘)
def resource_a():
    print(‘\nresources_a() "setup"‘)
 
def test_1_that_needs_resource_a(resource_a):
    print(‘test_1_that_needs_resource_a()‘)
 
def test_2_that_does_not():
    print(‘\ntest_2_that_does_not()‘)
 
def test_3_that_does(resource_a):
    print(‘test_3_that_does()‘)

即使我们在每个用例都继承了resource_a,但在实际测试中,所有用例只执行了一次resource_a

技术分享

这时,你可能会问,为什么只有setup功能,没有teardown功能,要teardown怎么写,方法如下:

def cheese_db(request):
    .....
   
    def teardown():
        print(‘\n[teardown] cheese_db finalizer, disconnect from db‘)
    request.addfinalizer(teardown)

这里使用的还是之前介绍的request.addfinalizer功能,函数名字可以任意取,不一定要teardown

 

5、带参数的fixture

这里就不介绍了,看官方文档

6、多种fixture scope结合使用

看代码,如下

@pytest.fixture(scope="module")
def foo(request):
    print(‘\nfoo setup - module fixture‘)
    def fin():
        print(‘foo teardown - module fixture‘)
    request.addfinalizer(fin)
 
@pytest.fixture()
def bar(request):
    print(‘bar setup - function fixture‘)
    def fin():
        print(‘bar teardown - function fixture‘)
    request.addfinalizer(fin)
 
@pytest.fixture()
def baz(request):
    print(‘baz setup - function fixture‘)
    def fin():
        print(‘baz teardown - function fixture‘)
    request.addfinalizer(fin)
 
def test_one(foo, bar, baz):
    print(‘in test_one()‘)
 
def test_two(foo, bar, baz):
    print(‘in test_two()‘)

测试结果如下

技术分享

 

参考文档:https://blog.csdn.net/hit_zhouyou/article/details/45100265

https://www.jianshu.com/p/552c3f1e9946

http://www.bubuko.com/infodetail-2292746.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值