既然有 setup 那就有 teardown,fixture 里面的 teardown 用 yield 来唤醒 teardown的执行

在所有用例执行完后执行:yield

import pytest

@pytest.fixture(scope='module')
def open():
print('打开浏览器=============') yield
print('执行teardown!!!')
print('最后关闭浏览器') def test_s1(open):
print('用例1111111') def test_s2(open):
print('用例22222222') def test_s3(open):
print('用例33333333') if __name__=='__main__':
pytest.main()

yield 遇到异常

1、如果其中一个用例出现异常,不影响 yield 后面的 teardown 执行,运行结果互不影响,并且全部用例执行完之后,yield 呼唤 teardown操作

import pytest

@pytest.fixture(scope='module')
def open():
print('\n打开浏览器=============') yield
print('执行teardown!!!')
print('最后关闭浏览器') def test_s1(open):
print('用例1111111') # 如果第一个用例异常了,不影响其他用的例执行
raise NameError # 模拟异常 def test_s2(open):
print('用例22222222') def test_s3(open):
print('用例33333333') if __name__=='__main__':
pytest.main()

2.如果在 setup 就异常了,那么是不会去执行 yield 后面的teardown 内容了

3.yield 也可以配合 with 语句使用,以下是官方文档给的案例

import smtplib
import pytest @pytest.fixture(scope='module')
def smtp():
with smtplib.SMTP('smtp.gmail.com') as smtp:
yield smtp # provide the fixture value

addfinalizer 方法终结函数

1、除了 yield 可以实现 teardown,在 request-context 对象中注册addfinalizer 方法也可以实现终结函数。
2、yield 和 addfinalizer 方法都是在测试完成后呼叫相应的代码。但是 addfinalizer 不同的是:
  1.他可以注册多个终结函数。
  2.返些终结方法总是会被执行,无论在之前的 setup code 有没有抛出错误。这个方法对于正确关闭所有的 fixture 创建的资源非常便利,即使其一在创建或获取时失败

import smtplib
import pytest @pytest.fixture(scope='module')
def smtp_connection(request):
smtp_connection = smtplib.SMTP('smtp.gmail.com', 587, timeout=5)
def fin():
print('teardown smtp_connection')
smtp_connection.close()
request.addfinalizer(fin)
return smtp_connection # provide the fixture value

pytest四:fixture_yield 实现 teardown的更多相关文章

  1. pytest的setup和teardown

    学过unittest的setup和teardown,前置和后置执行功能.pytest也有此功能并且功能更强大,今天就来学习一下吧. 用例运行级别: 模块级(setup_module/teardown_ ...

  2. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  3. 面向对象+unittest+pytest

    date:2018512+513 day07aft+day08mor 一.面向对象编程 1.定义类,类的继承 ps:与普通函数相比,在类中定义的函数第一个参数必须是类的本身实例变量self,在调用时, ...

  4. Pytest权威教程14-缓存:使用跨执行状态

    目录 缓存:使用跨执行状态 使用方法 首先只重新运行故障或故障 上次运行中没有测试失败时的行为 新的config.cache对象 检查缓存内容 清除缓存内容 逐步修复失败用例 unittest.Tes ...

  5. pytest框架结构运行规则及命名方式

    Pytest框架结构 Import pytest 类似的setup,teardown同样更灵活,还有个session() 模块级 (setup_module/teardown_module) 不在类中 ...

  6. 一文让你快速入门pytest框架

    pytest是什么 官方文档描述: pytest is a framework that makes building simple and scalable tests easy. Tests ar ...

  7. python自动化测试框架

    一.环境准备 1.python开发环境, python3.7 2.setuptools基础工具包 3.pip安装包管理工具 4.selenium自动化测试工具  chrom驱动下载地址: http:/ ...

  8. python单元测试框架笔记

    目录 单元测试概述 什么是单元测试 单元测试什么进行? 单元测试由谁负责? 单元测试需要注意 单元测试覆盖类型 python 单元测试框架 unittest pytest 测试框架 单元测试概述 什么 ...

  9. Python3+PyCharm+Django+Django REST framework开发教程

    一.说明 自己一是想跟上潮流二是习惯于直接干三是没有人可以请教,由于这三点经常搞得要死要活.之前只简单看过没写过Diango,没看过Django REST framework,今天一步到位直接上又撞上 ...

随机推荐

  1. 获取天气预报API

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  2. Spark记录-spark-env.sh配置

    环境变量 含义 SPARK_MASTER_IP master实例绑定的IP地址,例如,绑定到一个公网IP SPARK_MASTER_PORT mater实例绑定的端口(默认7077) SPARK_MA ...

  3. liunx必知必会(2)

    一.SSH免密登陆配置 1.相关概念 SSH 为 Secure Shell(安全外壳协议) 的缩写. 很多ftp.pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用 ...

  4. Sql Server数据库资料收集

    1.表分区 http://www.cnblogs.com/huangxincheng/p/3565755.html 2.MVP教程地址:http://www.cnblogs.com/lyhabc/p/ ...

  5. 17. Spring Boot 配置嵌入式Servlet容器

    一.如何定制和修改Servlet容器的相关配置 1.配置文件(ServerProperties): 优先级最高 server.port=8081 server.context‐path=/crud s ...

  6. mysql面试题分组并合并列

  7. android 不同Activity之间数据传递

    1. 传值Activity package mydemo.mycom.demo2; import android.content.Intent; import android.support.v7.a ...

  8. 进度条QProgressBar

    import sys from PyQt5.QtCore import Qt, QTimer from PyQt5.QtWidgets import QApplication, QWidget, QP ...

  9. Java EE之Hibernate异常总结【5】java.lang.StackOverflowError[栈溢出]

    Exception in thread "main" java.lang.StackOverflowError at java.lang.reflect.InvocationTar ...

  10. B - SETI POJ - 2065 (高斯消元)

    题目链接:https://vjudge.net/contest/276374#problem/B 题目大意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'* ...