pytest_06_fixture之yield实现teardown
上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作。
这里用到fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作
scope="module"
1.fixture参数scope="module",module作用是整个.py文件都会生效,用例调用时,参数写上函数名称就行
# coding:utf-8
import pytest @pytest.fixture(scope="module")
def open():
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") if __name__ == "__main__":
pytest.main(["-s", "test_f1.py"])
运行结果:
D:\Users\18630\AppData\Local\Programs\Python\Python36\python3.exe E:/Programs/ke4/pytest/learn_06.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: E:\Programs\ke4\pytest, inifile:
collected 3 items learn_06.py 打开浏览器,并且打开百度首页
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
. ========================== 3 passed in 0.09 seconds =========================== Process finished with exit code 0
从结果看出,虽然test_s1,test_s2,test_s3三个地方都调用了open函数,但是它只会在第一个用例前执行一次
2.如果test_s1不调用,test_s2(调用open),test_s3不调用,运行顺序会是怎样的?
# coding:utf-8
import pytest @pytest.fixture(scope="module")
def open():
print("打开浏览器,并且打开百度首页") def test_s1():
print("用例1:搜索python-1") def test_s2(open):
print("用例2:搜索python-2") def test_s3():
print("用例3:搜索python-3") if __name__ == "__main__":
pytest.main(["-s", "learn_06.py"])
运行结果:
D:\Users\18630\AppData\Local\Programs\Python\Python36\python3.exe E:/Programs/ke4/pytest/learn_06.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: E:\Programs\ke4\pytest, inifile:
collected 3 items learn_06.py 用例1:搜索python-1
.打开浏览器,并且打开百度首页
用例2:搜索python-2
.用例3:搜索python-3
. ========================== 3 passed in 0.09 seconds =========================== Process finished with exit code 0
从结果看出,module级别的fixture在当前.py模块里,只会在用例(test_s2)第一次调用前执行一次
yield执行teardown
1.前面讲的是在用例前加前置条件,相当于setup,既然有setup那就有teardown,fixture里面的teardown用yield来唤醒teardown的执行
# coding:utf-8
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") if __name__ == "__main__":
pytest.main(["-s", "test_f1.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\, inifile:
collected 3 items ..\..\..\..\..\..\test\test_f1.py 打开浏览器,并且打开百度首页
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
.执行teardown!
最后关闭浏览器 ========================== 3 passed in 0.01 seconds ===========================
yield遇到异常
1.如果其中一个用例出现异常,不影响yield后面的teardown执行,运行结果互不影响,并且全部用例执行完之后,yield呼唤teardown操作
# coding:utf-8
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") if __name__ == "__main__":
pytest.main(["-s", "test_f1.py"])
运行结果:
打开浏览器,并且打开百度首页
用例1:搜索python-1
F
open = None def test_s1(open):
print("用例1:搜索python-1") # 如果第一个用例异常了,不影响其他的用例执行
> raise NameError # 模拟异常
E NameError D:\test\test_f1.py:16: NameError
用例2:搜索python-2
.用例3:搜索python-3
.执行teardown!
最后关闭浏览器
2.如果在setup就异常了,那么是不会去执行yield后面的teardown内容了
3.yield也可以配合with语句使用,以下是官方文档给的案例
# 官方文档案例
# content of test_yield2.py 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方法也可以实现终结函数。
# 官方案例 # content of conftest.py
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
2.yield和addfinalizer方法都是在测试完成后呼叫相应的代码。但是addfinalizer不同的是:
他可以注册多个终结函数。
这些终结方法总是会被执行,无论在之前的setup code有没有抛出错误。这个方法对于正确关闭所有的fixture创建的资源非常便利,即使其一在创建或获取时失败
作者:含笑半步颠√
博客链接:https://www.cnblogs.com/lixy-88428977
声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。
pytest_06_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 5. fixture之yield实现teardown
前言: 1.前面讲的是在用例前加前置条件,相当于setup,既然有setup那就有teardown,fixture里面的teardown用yield来唤醒teardown的执行 看以下的代码: #!/ ...
- pytest四:fixture_yield 实现 teardown
既然有 setup 那就有 teardown,fixture 里面的 teardown 用 yield 来唤醒 teardown的执行 在所有用例执行完后执行:yield import pytest ...
- 学习-Pytest(五)yield操作
1.fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作 2.scope="module" 1.fixture参数scope=”modu ...
- 《带你装B,带你飞》pytest修仙之路5 - yield操作
1. 简介 上一篇中,我们刚刚实现了在每个用例之前执行初始化操作,那么用例执行完之后如需要清除数据(或还原)操作,可以使用 yield 来实现.fixture通过scope参数控制setup级别,既然 ...
- pytest(7)-yield与终结函数
通过上一篇文章,我们已经知道了pytest中,可以使用Fixture来完成运行测试用例之前的一些操作如连接数据库,以及测试执行之后自动去做一些善后工作如清空脏数据.关闭数据库连接等. 我们已经学会了f ...
- pytest进阶之fixture
前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...
- Pytest 测试框架
一 . Pytest 简介 Pytest是python的一种单元测试框架. 1. pytest 特点 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试 ...
随机推荐
- 《ELK Stack权威指南》读书笔记
Logstack: 1.Logstack介绍:Logstash is an open source data collection engine with real-time pipelining c ...
- SQLSERVER获取数据库中的所有表的名称、表中所有字段的属性
1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...
- tomcat找不到java_home
Tomcat Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 一眼就能看出来是jdk的环境有问题,但是用了 ...
- 【Gamma】Scrum Meeting 3
目录 写在前面 进度情况 任务进度表 Gamma阶段燃尽图 照片 写在前面 例会时间:5.27 22:30-23:30 例会地点:微信群语音通话 代码进度记录github在这里 临近期末,团队成员课程 ...
- ip rule实现源IP路由,实现一个主机多IP(或多网段)同时通(外部看是完全两个独立IP)
利用ip rule实现基于源地址区分路由表,实现一个主机多IP网段同时通.(外部的一个主机无论访问哪个网段都可以访问通)实际应用:创建路由表table200ip route add 192.168.1 ...
- LZW
LZW https://www2.cs.duke.edu/csed/curious/compression/lzw.html https://www.golangprograms.com/golang ...
- NPU TPU
https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet
- vue中axios使用二:axios以post,get,jsonp的方式请求后台数据
本文为博主原创,转载请注明出处 axios在上一篇中讲过:vue中axios使用一:axios做拦截器,axios是请求后台资源的模块,用来请求后台资源. axios本身是支持get,post请求后台 ...
- rabbitMQ消息队列 – Message方法解析
消息的创建由AMQPMessage对象来创建$message = new AMQPMessage("消息内容");是不是很简单. 后边是一个数组.可以对消息进行一些特殊配置$mes ...
- Python的log
关键代码 调用方: from Logger import MyLogger import logging import sys, os def getLogger(): # get the file ...