【pytest官方文档】解读fixtures - 7. Teardown处理,yield和addfinalizer
当我们运行测试函数时,我们希望确保测试函数在运行结束后,可以自己清理掉对环境的影响。
这样的话,它们就不会干扰任何其他的测试函数,更不会日积月累的留下越来越多的测试数据。
用过unittest的朋友相信都知道teardown这个函数,做的是一样的事情,那么下面姑且就把这种“善后”工作的代码
叫做teardown代码吧。
而pytest中的fixture,也提供了这样一个非常有用的系统,我们可以在里面定义teardown代码。
这里可以使用2种方式来实现,分别是yield
和addfinalizer
一、yield fixtures(推荐)
1, yield 和 return
在有yield
的fixtures函数中,关键字yield
可以代替 return
,可以把fixture里的一些对象传递给调用它们的fixture函数或者测试函数。
就像其他普通的fixture函数一样。区别仅仅是:
yield
替换掉了return
- teardown代码放置在
yield
之后
2, yield的执行顺序
pytest在执行fixture函数时,会根据fixture函数之间的线性关系顺序调用的。
但是,当测试函数运行结束的时候,pytest又会按照之前的顺序反方向来执行fixture中yield之后的代码。
结合示例看下,这里没有引用官方示例了,手写一个直观些的:
import pytest
@pytest.fixture
def fixture_one():
print("\n执行fixture_one")
return 1
@pytest.fixture
def fixture_two(fixture_one):
print("\n执行fixture_two")
yield 2
print("\n执行fixture_two的teardown代码")
@pytest.fixture
def fixture_adding(fixture_one, fixture_two):
print("\n执行fixture_adding")
result = fixture_one + fixture_two
yield result
print("\n执行fixture_adding的teardown代码")
def test_demo(fixture_two, fixture_adding):
print("\n执行测试函数test_demo")
assert fixture_adding == 3
代码中,fixture中调用多个fixture,测试函数中调用多个fixture,通过前面几章的接触,
相信大家这时候已经可以梳理出前后调用顺序了:
- test_demo 测试函数,先去调用fixture函数 fixture_two,然后调用 fixture_adding。
- 在fixture函数 fixture_two中,又会去调用另一个fixture函数 fixture_one。
- 在fixture函数 fixture_adding中,调用了 fixture_one、fixture_two。
所以,fixture函数的先后顺序是:fixture_one
、fixture_two
、fixture_adding
。
那么,可以得知测试结束后的teardown代码执行顺序:fixture_adding
、fixture_two
。
运行一下代码,验证下结果是否符合我们的梳理:
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: D:\练习\demo_fixture
plugins: allure-pytest-2.8.32, celery-4.3.0, Faker-4.14.2, base-url-1.4.2, html-2.1.1, metadata-1.10.0collected 1 item
test_module.py
执行fixture_one
执行fixture_two
执行fixture_adding
.
执行测试函数test_demo
执行fixture_adding的teardown代码
执行fixture_two的teardown代码
[100%]
============================== 1 passed in 0.09s ==============================
结果与我们刚才梳理的一致。
但是,值得注意的是,就算是teardown的代码是按照正确的顺序执行,也不能保证代码能正常执行的。
比如说teardown里的某些代码执行异常了,导致别的清理动作也没法执行。
这里就涉及到另一个点了:健壮的fixture结构应该是什么样子。这个官方文档另起进行说明,这里同样。
二、addfinalizer
1.request.addfinalizer把函数变成终结器
在pytest中想要做teardown的处理,除了使用带有yield的fixture函数,还可以直接添加终结器。
直接来看示例代码:
import pytest
@pytest.fixture()
def demo_fixture(request):
print("\n这个fixture在每个case前执行一次")
def demo_finalizer():
print("\n在每个case完成后执行的teardown")
#注册demo_finalizer为终结函数
request.addfinalizer(demo_finalizer)
def test_01(demo_fixture):
print("\n===执行了case: test_01===")
def test_02(demo_fixture):
print("\n===执行了case: test_02===")
看下运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: D:\练习\demo_fixture
plugins: allure-pytest-2.8.32, celery-4.3.0, Faker-4.14.2, base-url-1.4.2, html-2.1.1, metadata-1.10.0collected 2 items
test_module.py
这个fixture在每个case前执行一次
.
===执行了case: test_01===
在每个case完成后执行的teardown
这个fixture在每个case前执行一次
.
===执行了case: test_02===
在每个case完成后执行的teardown
[100%]
============================== 2 passed in 0.10s ==============================
Process finished with exit code 0
运行结果可以看出,效果与yield是一致的。这算是一个固定写法,关于request
文档中也有另外的讲解,届时再分享。
2.request.addfinalizer注册多个终结器函数
上方代码是一个终结函数,如果要注册多个呢?
import pytest
@pytest.fixture()
def demo_fixture(request):
print("\n这个fixture在每个case前执行一次")
def demo_finalizer():
print("\n在每个case完成后执行的teardown")
def demo_finalizer2():
print("\n在每个case完成后执行的teardown2")
#注册demo_finalizer为终结函数
request.addfinalizer(demo_finalizer)
request.addfinalizer(demo_finalizer2)
def test_01(demo_fixture):
print("\n===执行了case: test_01===")
def test_02(demo_fixture):
print("\n===执行了case: test_02===")
if __name__ == '__main__':
pytest.main(['-s', 'test_module.py'])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: D:\练习\demo_fixture
plugins: allure-pytest-2.8.32, celery-4.3.0, Faker-4.14.2, base-url-1.4.2, html-2.1.1, metadata-1.10.0collected 2 items
test_module.py
这个fixture在每个case前执行一次
.
===执行了case: test_01===
在每个case完成后执行的teardown2
在每个case完成后执行的teardown
这个fixture在每个case前执行一次
.
===执行了case: test_02===
在每个case完成后执行的teardown2
在每个case完成后执行的teardown
[100%]
============================== 2 passed in 0.09s ==============================
Process finished with exit code 0
这里要注意的是,多个终结器的情况下,执行的顺序是与注册时候相反的。
3.yield和addfinalizer的区别
目前从官方文档中看到的是
We have to be careful though, because pytest will run that finalizer once it’s been added,
even if that fixture raises an exception after adding the finalizer.
一旦添加了终结器,pytest便会执行。
但是,当我尝试在setup代码中进行抛错,终结器的代码却并没有执行。
尝试搜索外网暂时也没得到有效的帮助,只能在GitHub上向pytest提了issue了,这里算是埋下一个坑,待后续解决。
【pytest官方文档】解读fixtures - 7. Teardown处理,yield和addfinalizer的更多相关文章
- 【pytest官方文档】解读fixtures - 3. fixtures调用别的fixtures、以及fixture的复用性
pytest最大的优点之一就是它非常灵活. 它可以将复杂的测试需求简化为更简单和有组织的函数,然后这些函数可以根据自身的需求去依赖别的函数. fixtures可以调用别的fixtures正是灵活性的体 ...
- 【pytest官方文档】解读fixtures - 1.什么是fixtures
在深入了解fixture之前,让我们先看看什么是测试. 一.测试的构成 其实说白了,测试就是在特定的环境.特定的场景下.执行特定的行为,然后确认结果与期望的是否一致. 就拿最常见的登录来说,完成一次正 ...
- 【pytest官方文档】解读fixtures - 2. fixtures的调用方式
既然fixtures是给执行测试做准备工作的,那么pytest如何知道哪些测试函数 或者 fixtures要用到哪一个fixtures呢? 说白了,就是fixtures的调用. 一.测试函数声明传参请 ...
- Cuda 9.2 CuDnn7.0 官方文档解读
目录 Cuda 9.2 CuDnn7.0 官方文档解读 准备工作(下载) 显卡驱动重装 CUDA安装 系统要求 处理之前安装的cuda文件 下载的deb安装过程 下载的runfile的安装过程 安装完 ...
- 【pytest官方文档】解读fixtures - 10. fixture有效性、跨文件共享fixtures
一.fixture有效性 fixture有效性,说白了就是fixture函数只有在它定义的使用范围内,才可以被请求到.比如,在类里面定义了一个fixture, 那么就只能是这个类中的测试函数才可以请求 ...
- 【pytest官方文档】解读fixtures - 8. yield和addfinalizer的区别(填坑)
在上一章中,文末留下了一个坑待填补,疑问是这样的: 目前从官方文档中看到的是 We have to be careful though, because pytest will run that fi ...
- 【pytest官方文档】解读- 插件开发之hooks 函数(钩子)
上一节讲到如何安装和使用第三方插件,用法很简单.接下来解读下如何自己开发pytest插件. 但是,由于一个插件包含一个或多个钩子函数开发而来,所以在具体开发插件之前还需要先学习hooks函数. 一.什 ...
- 【pytest官方文档】解读- 开发可pip安装的第三方插件
在上一篇的 hooks 函数分享中,开发了一个本地插件示例,其实已经算是在编写插件了.今天继续跟着官方文档学习更多知识点. 一个插件包含一个或多个钩子函数,pytest 正是通过调用各种钩子组成的插件 ...
- FluentValidation:C#后端输入验证框架的官方文档解读
参照 FluentValidation 的官方文档写的例子,方便日后查看和使用. 原文:https://github.com/JeremySkinner/FluentValidation/wiki H ...
随机推荐
- uestc 1221 Ancient Go
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status Y ...
- 阿里云 MaxCompute(ODPS)
大数据产品架构 BASE - Dataworks ODPS - MaxCompute ODPS 功能组成(Open Data Process Service) ODPS 是旧称,阿里云公有云服务中现称 ...
- HDU - 5115 Dire Wolf (非原创)
Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not ...
- liunx命令二
声明:以下资料全部摘自实验楼 常用快捷键 按键 作用 Table 补全命令 Ctrl+c 强制结束 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂停当前程序,暂停后按下任意键恢复运行 Ctrl ...
- JavaScript基本包装类介绍
为了便于操作基本类型值,ECMAScript 提供了 3 个特殊的引用类型:Boolean.Number和 String.这些类型与其他引用类型相似,但同时也具有与各自的基本类型相应的特殊行为.实际上 ...
- (数据科学学习手札107)在Python中利用funct实现链式风格编程
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 链式编程是一种非常高效的组织代码的方式,典型如p ...
- 用python写的一个自动卸载python包的脚本
import osplist=os.popen("pip list") # 执行windows cmd命令,获取所有包package列表,并获取返回结果到plist#跳过第1,2行 ...
- ES6 Class vs ES5 constructor function All In One
ES6 Class vs ES5 constructor function All In One ES6 类 vs ES5 构造函数 https://developer.mozilla.org/en- ...
- Web 安全 & 反爬虫原理
Web 安全 & 反爬虫原理 数据加密/解密 HTTPS ip 封锁 请求限制 爬虫识别,canvas 指纹 refs https://segmentfault.com/a/119000001 ...
- React Native & CodePush & App Center
React Native & CodePush & App Center https://docs.microsoft.com/en-us/appcenter/distribution ...