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 ...
随机推荐
- python学习笔记(4)-基本数据类型-数字类型及操作
大学mooc 北京理工大学 python语言程序设计课程学习笔记 一.整数类型 可正可负,没有取值范围的限制(这个与c不同,c要考虑数据类型的存储空间).如pow(x,y),计算x的y次方,pow(2 ...
- python3 写的一个压测脚本(有待开发)
import requests import queue import threading import time status_code_list = [] exec_time = 0 class ...
- python设计模式第二十三天【状态模式】
1.应用场景 (1)通过改变对象的内部状态从而改变对象的行为,一般表现为状态的顺序执行 2.代码实现 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ from ...
- 八、.net core 通过数据库配置文件连接操作数据库
一.创建DotNetCore项目 直接创建core项目并不勾选docker支持 二.nuget进行连接MySQL程序集的下载安装 1.MySql.Data.EntityFrameworkCore方式 ...
- 一、使用Navicat连接阿里云服务器宝塔面板里创建的数据库
一.数据库配置连接 (通过新增用户的方式)
- Python——进程通信之间数据共享
from multiprocessing import Manager,Process,Lock def main(dic,lock): lock.acquire() dic['count'] -= ...
- c++ string替换指定字符串
string fnd = "dataset"; string rep = "labels"; string buf = "d:/data/datase ...
- Asp.Net Core 输出 Word
In one of the ASP.NET Core projects we did in the last year, we created an OutputFormatter to provid ...
- Nginx 如何限制响应速度
在 location 里设置 location { set $limit_rate 1k; 表示每秒只响应1k的速度 }
- Nginx 磁盘IO的优化
L:132