pytest 5. fixture之yield实现teardown
前言:
1.前面讲的是在用例前加前置条件,相当于setup,既然有setup那就有teardown,fixture里面的teardown用yield来唤醒teardown的执行
看以下的代码:
#!/usr/bin/env/python
# -*-coding:utf-8-*-
# authour:xiapmin_pei import pytest @pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页") yield
print("执行teardown!")
print("最后关闭浏览器") def test_s1(open):
print("用例1:搜索python-1") def test_s2(open):
print("用例2:搜索python-2") def test_s3(open):
print("用例3:搜索python-3") 执行结果:yield之后的代码在最后才运行。

yield遇到异常:
1.如果其中一个用例出现异常,不影响yield后面的teardown执行,运行结果互不影响,并且在用例全部执行完之后,会呼唤teardown的内容
#!/usr/bin/env/python
# -*-coding:utf-8-*-
# authour:xiapmin_pei import pytest @pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页") yield
print("执行teardown!")
print("最后关闭浏览器") def test_s1(open):
print("用例1:搜索python-1")
#如果第一个用例异常了,不影响其他的用例执行
raise NameError # 模拟异常 def test_s2(open):
print("用例2:搜索python-2") def test_s3(open):
print("用例3:搜索python-3") 运行结果:
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 3 items
test_mod.py 打开浏览器,并且打开百度首页
F用例1:搜索python-1
test_mod.py:14 (test_s1)
open = None
def test_s1(open):
print("用例1:搜索python-1")
#如果第一个用例异常了,不影响其他的用例执行
> raise NameError # 模拟异常
E NameError
test_mod.py:18: NameError
.用例2:搜索python-2
.用例3:搜索python-3
执行teardown!
最后关闭浏览器
[100%]
=================================== FAILURES ===================================
___________________________________ test_s1 ____________________________________
open = None
def test_s1(open):
print("用例1:搜索python-1")
#如果第一个用例异常了,不影响其他的用例执行
> raise NameError # 模拟异常
E NameError
test_mod.py:18: NameError
---------------------------- Captured stdout setup -----------------------------
打开浏览器,并且打开百度首页
----------------------------- Captured stdout call -----------------------------
用例1:搜索python-1
=============================== warnings summary ===============================
<undetermined location>
pytest-catchlog plugin has been merged into the core, please remove it from your requirements.
-- Docs: http://doc.pytest.org/en/latest/warnings.html
================ 1 failed, 2 passed, 1 warnings in 0.07 seconds ================
Process finished with exit code 0
2.如果在setup就异常了,那么是不会去执行yield后面的teardown内容了
#!/usr/bin/env/python
# -*-coding:utf-8-*-
# authour:xiapmin_pei import pytest @pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
raise NameError # 模拟异常 yield
print("执行teardown!")
print("最后关闭浏览器") def test_s1(open):
print("用例1:搜索python-1")
#如果第一个用例异常了,不影响其他的用例执行
raise NameError # 模拟异常 def test_s2(open):
print("用例2:搜索python-2") def test_s3(open):
print("用例3:搜索python-3") 运行结果:
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 3 items
test_mod.py E打开浏览器,并且打开百度首页
test setup failed
@pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
> raise NameError # 模拟异常
E NameError
test_mod.py:10: NameError
E
test setup failed
@pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
> raise NameError # 模拟异常
E NameError
test_mod.py:10: NameError
E
test setup failed
@pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
> raise NameError # 模拟异常
E NameError
test_mod.py:10: NameError
[100%]
==================================== ERRORS ====================================
__________________________ ERROR at setup of test_s1 ___________________________
@pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
> raise NameError # 模拟异常
E NameError
test_mod.py:10: NameError
---------------------------- Captured stdout setup -----------------------------
打开浏览器,并且打开百度首页
__________________________ ERROR at setup of test_s2 ___________________________
@pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
> raise NameError # 模拟异常
E NameError
test_mod.py:10: NameError
__________________________ ERROR at setup of test_s3 ___________________________
@pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页")
> raise NameError # 模拟异常
E NameError
test_mod.py:10: NameError
=============================== warnings summary ===============================
<undetermined location>
pytest-catchlog plugin has been merged into the core, please remove it from your requirements.
-- Docs: http://doc.pytest.org/en/latest/warnings.html
===================== 1 warnings, 3 error in 0.08 seconds ======================
Process finished with exit code 0
pytest 5. fixture之yield实现teardown的更多相关文章
- pytest自动化4:fixture之yield实现teardown
出处:https://www.cnblogs.com/yoyoketang/p/9401554.html 前言: 上一篇介绍了fixture通过scope参数控制setup级别,我们一起来温故下fix ...
- pytest文档6-fixture之yield实现teardown
前言 上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作. 这里用到fixture的teardown操作并 ...
- pytest.4.Fixture
From: http://www.testclass.net/pytest/fixture/ 我们可以简单的把Fixture理解为准备测试数据和初始化测试对象的阶段. 一般我们对测试数据和测试对象的管 ...
- pytest_06_fixture之yield实现teardown
上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作. 这里用到fixture的teardown操作并不是独 ...
- 『德不孤』Pytest框架 — 12、Pytest中Fixture装饰器(二)
目录 5.addfinalizer关键字 6.带返回值的Fixture 7.Fixture实现参数化 (1)params参数的使用 (2)进阶使用 8.@pytest.mark.usefixtures ...
- pytest(6)-Fixture(固件)
什么是固件 Fixture 翻译成中文即是固件的意思.它其实就是一些函数,会在执行测试方法/测试函数之前(或之后)加载运行它们,常见的如接口用例在请求接口前数据库的初始连接,和请求之后关闭数据库的操作 ...
- pytest 3.fixture介绍一 conftest.py
前言: 前面一篇pytest2 讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录, ...
- pytest的fixture怎么用?
文章总览图 fixture和unittest是冲突的.舍弃unittest只用pytest. 会遇到在很多用例当中,它的前置条件是长得一样的.用例写的越来越多的时候,肯定会遇到前置条件都差不多,大家差 ...
- pytest框架: fixture之conftest.py
原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown ...
随机推荐
- Prism框架研究(一)
从今天起开始写一个Prism框架的学习博客,今天是第一篇,所以从最基本的一些概念开始学习这个基于MVVM的框架的学习,首先看一下Prism代表什么,这里引用一下比较官方的英文解释来看一下:Prism ...
- Http请求笔记
1 HTTP请求报文组成: 请求行:请求方法 url 协议版本 请求头:报文头-属性名:属性值 Accept属性告诉服务端-客户端接受什么类型的响应,可为一个或多个mime类型值 Cookie:服务端 ...
- java 中的Collection
/* *一. Collection?-------->容器! * * 1.来源于java.util包 非常实用的数据结构! * *二. 方法? * * void clear()删除集合中所有元素 ...
- 百度云虚拟主机配置 Thinkphp5.1
材料 服务器:百度云虚拟主机(nginx+php7.0+linux) Thinkphp 5.1 问题 百度云默认目录为/webroot,但是我们的需求是将项目存放到/webroot/public下面. ...
- HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别
HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...
- springmvc拦截器匹配规则
- 当页面是动态时 如果后台存储id可以通过查询后台方式获取对象;当后台没有存储时候 只有通过前端标记了 例如标记数量为10 我们根据传递过来的10循环取值
当页面是动态时 如果后台存储id可以通过查询后台方式获取对象;当后台没有存储时候 只有通过前端标记了 例如标记数量为10 我们根据传递过来的10循环取值
- bzoj1206-[HNOI2005]虚拟内存
卡读的毒瘤题== 看懂之后用map模拟.或者线段树 #include<cstdio> #include<iostream> #include<cmath> #inc ...
- Nginx 防盗链 secure_link 模块
L:76 需要通过 --with-http_secure_link_module 编译进Nginx secure_link 指令 Syntax: secure_link expression; Def ...
- Tyche 2147 旅行
题目描述 你有m元钱,将要游览n个国家.每一个国家有一种商品,其中第i个国家商品的单价为ai元.每到一个国家,你会用手上的钱疯狂购买这个国家的商品,直到剩余的钱无法购买为止. 现在你要决定游览这n个国 ...