前言:

  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的更多相关文章

  1. pytest自动化4:fixture之yield实现teardown

    出处:https://www.cnblogs.com/yoyoketang/p/9401554.html 前言: 上一篇介绍了fixture通过scope参数控制setup级别,我们一起来温故下fix ...

  2. pytest文档6-fixture之yield实现teardown

    前言 上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作. 这里用到fixture的teardown操作并 ...

  3. pytest.4.Fixture

    From: http://www.testclass.net/pytest/fixture/ 我们可以简单的把Fixture理解为准备测试数据和初始化测试对象的阶段. 一般我们对测试数据和测试对象的管 ...

  4. pytest_06_fixture之yield实现teardown

    上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作. 这里用到fixture的teardown操作并不是独 ...

  5. 『德不孤』Pytest框架 — 12、Pytest中Fixture装饰器(二)

    目录 5.addfinalizer关键字 6.带返回值的Fixture 7.Fixture实现参数化 (1)params参数的使用 (2)进阶使用 8.@pytest.mark.usefixtures ...

  6. pytest(6)-Fixture(固件)

    什么是固件 Fixture 翻译成中文即是固件的意思.它其实就是一些函数,会在执行测试方法/测试函数之前(或之后)加载运行它们,常见的如接口用例在请求接口前数据库的初始连接,和请求之后关闭数据库的操作 ...

  7. pytest 3.fixture介绍一 conftest.py

    前言: 前面一篇pytest2 讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录, ...

  8. pytest的fixture怎么用?

    文章总览图 fixture和unittest是冲突的.舍弃unittest只用pytest. 会遇到在很多用例当中,它的前置条件是长得一样的.用例写的越来越多的时候,肯定会遇到前置条件都差不多,大家差 ...

  9. pytest框架: fixture之conftest.py

    原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown ...

随机推荐

  1. 在windows 7 和linux上安装xlwt和xlrd

    在windows 7上安装xlwt xlrd xlwt是开源社区编写的python库,需要单独安装,下载地址https://pypi.python.org/pypi/xlwt 目前xlwt最新的版本是 ...

  2. Yii2总结

    1. Web访问流程(即在浏览器中输入一个网址至浏览器展现页面结果的过程) a. 将输入的网址提取出域名,在本地hosts文件中查找对应的IP地址(windows为C:/windows/system3 ...

  3. 存储过程中的 SET XACT_ABORT ON 和事务

    在存储过程中写SET XACT_ABORT ON 有什么用? SET XACT_ABORT ON是设置事务回滚的!当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚为OFF时,只回滚 ...

  4. rmse均方根误差

    rmse=sqrt(sum((w-r).^2)/length(w))

  5. JS 单线程和事件循环

    Js 是单线程,js代码从上到下依次执行,比如我们写了两个函数,肯定是上面的函数先执行,下面的函数后执行.但是这种单线程有一个非常大的问题,那就是遇到耗时的任务,后面的任务只能等待它执行完,才能进行. ...

  6. luogu T40984Chino的成绩

    Chino的成绩 题目描述 Chino非常注重自己的成绩 Chino有m种方式给自己增加rp以增加成绩,她的每种增加rp的方式都有n个阶段,第i种的第j个阶段增加的rp表示为Aij​,表示连续进行了j ...

  7. Nginx 针对上游服务器缓存

    L:99 nginx缓存 : 定义存放缓存的载体 proxy_cache 指令 Syntax: proxy_cache zone | off; Default: proxy_cache off; Co ...

  8. delegate--委托

    delegate--委托 (可以把委托看成用来执行方法的一个东西) eg: namespace delegateTest{ delegate double MathsOp(double x); cla ...

  9. jsp页面中 <%%> <%! %>, <%=%> <%-- --%>有什么区别

    <%%> 可添加java代码片段   <%! %>       可添加java方法   <%=%>       变量或表达式值输出到页面 <%-- --%&g ...

  10. Paths on a Grid POJ - 1942 组合数学 (组合数的快速计算)

    题意:格路问题 没什么难度 难点在于如何快速计算相对较大的组合数 思路:运用手写计算组合数的方式进行计算  如c(8,3) 如果手算就是   8*7*6/(3*2*1)这样可以很快得解出 计算代码为: ...