引入
我们之前学习的都是测试用例的前置固件,也就是相当于“setup”。说到这,细心的你可能想到了,那有没有什么方式可以表示出“teardown”?这就是我们今天学习的yield和addfinalizer。

yield
yield是一个关键字,它不是单独存在的,要写在fixtrue标记的固件中。

我们在声明的固件myfixture中加入yield关键字,在它下面写测试用例执行后想要运行的代码;其他有关于固件的使用没有任何差别。需要说明的一点是我们在pytest主函数中增加了一个参数“–setup-show”,他会显示出固件的执行情况。

import pytest@pytest.fixture()
def myfixture():print("执行myfixture前半部分")yieldprint("执行myfixture的后半部分")class Test_firstFile():def test_one(self,myfixture):print("执行test_one")assert 1+2==3def test_two(self):print("执行test_two")assert 1==1def test_three(self):print("执行test_three")assert 1+1==2if __name__=="__main__":pytest.main(["--setup-show","-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 itemstest03.py 执行myfixture前半部分SETUP    F myfixturetest03.py::Test_firstFile::test_one (fixtures used: myfixture)执行test_one
.执行myfixture的后半部分TEARDOWN F myfixturetest03.py::Test_firstFile::test_two执行test_two
.test03.py::Test_firstFile::test_three执行test_three
.============================== 3 passed in 0.03s ==============================Process finished with exit code 0

fixture里面的teardown用yield来唤醒teardown的执行

@pytest.fixture(scope="function",autouse=True)
def open():print("打开浏览器,并且打开百度首页")yieldprint("执行teardown!")def test_s1(open):print("用例1:搜索python-1",open)def test_s2():print("用例2:搜索python-2")def test_s3():print("用例3:搜索python-3")if __name__=="__main__":pytest.main(["--setup-show","-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 itemstest03.py 打开浏览器,并且打开百度首页SETUP    F opentest03.py::test_s1 (fixtures used: open)用例1:搜索python-1 None
.执行teardown!TEARDOWN F open打开浏览器,并且打开百度首页SETUP    F opentest03.py::test_s2 (fixtures used: open)用例2:搜索python-2
.执行teardown!TEARDOWN F open打开浏览器,并且打开百度首页SETUP    F opentest03.py::test_s3 (fixtures used: open)用例3:搜索python-3
.执行teardown!TEARDOWN F open============================== 3 passed in 0.03s ==============================Process finished with exit code 0

如果测试用例中的代码出现异常或者断言失败,并不会影响他的固件中yield后的代码执行;但是如果固件中的yield之前的代码也就是相当于setup部分的带代码,出现错误或断言失败,那么yield后的代码将不会再执行,当然测试用例中的代码也不会执行。

@pytest.fixture(scope="function",autouse=True)
def open():print("打开浏览器,并且打开百度首页")yieldprint("执行teardown!")def test_s1(open):print("用例1:搜索python-1",aaaa)def test_s2():print("用例2:搜索python-2")def test_s3():print("用例3:搜索python-3")if __name__=="__main__":pytest.main(["--setup-show","-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 itemstest03.py 打开浏览器,并且打开百度首页SETUP    F opentest03.py::test_s1 (fixtures used: open)F执行teardown!TEARDOWN F open打开浏览器,并且打开百度首页SETUP    F opentest03.py::test_s2 (fixtures used: open)用例2:搜索python-2
.执行teardown!TEARDOWN F open打开浏览器,并且打开百度首页SETUP    F opentest03.py::test_s3 (fixtures used: open)用例3:搜索python-3
.执行teardown!TEARDOWN F open================================== FAILURES ===================================
___________________________________ test_s1 ___________________________________open = Nonedef test_s1(open):
>       print("用例1:搜索python-1",aaaa)
E       NameError: name 'aaaa' is not definedtest03.py:78: NameError
========================= 1 failed, 2 passed in 0.13s =========================

我们也可以通过request.addfinalizer()的方式实现“teardown”

我们在固件中传入request参数;又在固件中定义了一个内置函数;最后将定义的内置函数添加到request的addfinalizer中

@pytest.fixture()
def myfixture(request):print ("执行固件myfixture的前半部分")def myteardown():print("执行固件myfture的后半部分")request.addfinalizer(myteardown)class Test_Pytest():def test_one(self,myfixture):print("test_one方法执行" )assert 1==1def test_two(self):print("test_two方法执行" )assert "o" in "love"def test_three(self):print("test_three方法执行" )assert 3-2==1if __name__=="__main__":pytest.main(["--setup-show","-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 itemstest03.py 执行固件myfixture的前半部分SETUP    F myfixturetest03.py::Test_Pytest::test_one (fixtures used: myfixture)test_one方法执行
.执行固件myfture的后半部分TEARDOWN F myfixturetest03.py::Test_Pytest::test_twotest_two方法执行
.test03.py::Test_Pytest::test_threetest_three方法执行
.============================== 3 passed in 0.04s ==============================Process finished with exit code 0

Python Pytest中fixture之yield唤醒teardown和终结函数addfinalizer相关推荐

  1. 【pytest】(六) pytest中fixture的使用

    上篇文章中提到了,在pytest中的fixture可以完成unitest中setUp.tearDown.今天就来详细看一下. 一.pytest中的fixture是什么 为可靠的和可重复执行的测试提供固 ...

  2. pytest中fixture的使用

    前言 前面一篇讲了setup.teardown可以实现在执行用例前或结束后加入一些操作,但这种都是针对整个脚本全局生效的 如果有以下场景:用例 1 需要先登录,用例 2 不需要登录,用例 3 需要先登 ...

  3. Pytest中fixture夹具

    Unittest和Pytest前后置区别 这里抽用例前置与后置的区别来讲,先看unittest的前后置使用: import unittestclass TestFixtures01(unittest. ...

  4. pytest系列——fixture之yield关键字实现teardown用例后置操作

    fixture之yield关键字实现teardown用例后置操作 前言 ①pytest之fixture函数使用(pytest测试框架测试固件 文中讲到,fixture函数是通过scope参数来控制se ...

  5. python:pytest中的setup和teardown

    原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...

  6. Python Pytest前置setup和后置teardown详解

    pytest用例运行级别: ●模块级(setup_module/teardown_module)开始于模块始末,全局的 ●函数级(setup_function/teardown_function)只对 ...

  7. Python Pytest调用fixture之@pytest.mark.usefixtures()、叠加usefixtures、@pytest.fixture(autouse=True)用法详解

    usefixtures与传fixture区别  如果fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别. 当fix ...

  8. 在python子程序中、使用关键字_Python 的控制和函数

    控制if else for while 函数 函数的定义 函数一词来源于数学,但编程中的函数概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BAS ...

  9. Pytest之fixture

    VOL 148 10 2020-08 今天距2021年143天 这是ITester软件测试小栈第148次推文 点击上方蓝字"ITester软件测试小栈"关注我,每周一.三.五早上  ...

最新文章

  1. Crawler:基于urllib库获取cn-proxy代理的IP地址
  2. ethtool编译与内核实现介绍
  3. HDU6428-Calculate-数论函数
  4. 机器人动力学与控制_力控制与位置控制的区别
  5. 从图片搜索到人脸识别,CV正在成为“互动营销”领域的【硬核技术】
  6. php伪协议漏洞_PHP之伪协议深入理解
  7. 10位photoshop顶尖设计大师
  8. oracle tirger_TPS65130RGETG4_驱动_中文手册(3/10)_TI - 万联芯城
  9. excel函数调用其他sheet单元格
  10. 零基础使用pscc 证件照换底色
  11. 数据库基础-update语句详解
  12. 系统之家 linux下载,迅雷Linux版下载_迅雷Linux版官方版1.0.0.1 - 系统之家
  13. 产品经理面试(题目+答案)
  14. Windows Server 2008 R2 安装及配置
  15. warning: variable ‘a‘ set but not used [-Wunused-but-set-variable]
  16. 电脑的锁屏密码忘记了怎么办?
  17. Mybatis实现同时传入对象参数和字符串参数
  18. 利用Matlab替换图片部分颜色
  19. deepin20.7隐藏分区
  20. STI、LOD与WPE概念:形成机理及对电路设计的影响

热门文章

  1. MySQL如何创建沙箱,沙箱环境搭建 - osc_y8w65yuq的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. c语言打印字符的函数参数,C语言格式化打印函数vsnprintf()的实现
  3. 如何修改matlab中的语句,求大神帮忙看一下这个语句怎么改!!!!!
  4. Spring bean 不被 GC 的真正原因
  5. matlab guide对话框+滑动条+弹出式菜单+列表框的使用
  6. php class使用方法,PHP调试类Krumo使用教程
  7. oracle 28000错误码,Oracle数据库 ORA-28000 错误处理方式
  8. HTML+CSS+JS实现 ❤️绘制卡丁车动漫特效❤️
  9. 写得好的html网页,优化网站排名-使用Markdown编写更好的内容和HTML
  10. suse linux 创建用户密码,suse linux上创建用户方式