美文网首页java8
学习笔记-Pytest(五)yield操作

学习笔记-Pytest(五)yield操作

作者: sofiiii | 来源:发表于2019-02-01 16:50 被阅读0次

1. 前言


fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作

2. scope=”module”


1.fixture参数scope=”module”,module作用是整个.py文件都会生效( 整个文件只会执行一次),用例调用时,参数写上函数名称就行

# 新建一个文件test_f1.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):  # 不传login
    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

..\..\..\..\..\..\YOYO\test_f1.py 打开浏览器,并且打开百度首页
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
.

========================== 3 passed in 0.01 seconds ===========================

从结果看出,虽然test_s1,test_s2,test_s3三个地方都调用了open函数,但是它只会在第一个用例前执行一次

2.如果test_s1不调用,test_s2(调用open),test_s3不调用,运行顺序会是怎样的?

# 新建一个文件test_f1.py
# coding:utf-8
import pytest

@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并且打开百度首页")

def test_s1():
    print("用例1:搜索python-1")

def test_s2(open):  # 不传login
    print("用例2:搜索python-2")

def test_s3():
    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

..\..\..\..\..\..\YOYO\test_f1.py 用例1:搜索python-1
.打开浏览器,并且打开百度首页
用例2:搜索python-2
.用例3:搜索python-3
.

========================== 3 passed in 0.01 seconds ===========================

从结果看出,module级别的fixture在当前.py模块里,只会在用例(test_s2)第一次调用前执行一次

3. yield执行teardown


1.fixture里面的teardown用yield来唤醒teardown的执行

# 新建一个文件test_f1.py
# 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):  # 不传login
    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

..\..\..\..\..\..\YOYO\test_f1.py 打开浏览器,并且打开百度首页
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
.执行teardown!
最后关闭浏览器

========================== 3 passed in 0.01 seconds ===========================

3. yield遇到异常


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

# 新建一个文件test_f1.py
# 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):  # 不传login
    print("用例2:搜索python-2")

def test_s3(open):
    print("用例3:搜索python-3")

if __name__ == "__main__":
    pytest.main(["-s", "test_f1.py"])

运行结果:

\YOYO\test_f1.py 打开浏览器,并且打开百度首页
用例1:搜索python-1
F
open = None

    def test_s1(open):
        print("用例1:搜索python-1")

        # 如果第一个用例异常了,不影响其他的用例执行
>       raise NameError  # 模拟异常
E       NameError

D:\YOYO\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

相关文章

  • 学习笔记-Pytest(五)yield操作

    1. 前言 fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作 2....

  • 笔记8:yield和addfinalizer

    yield import pytest @pytest.fixture() def login(): print(...

  • Pytest学习笔记(非完整版)

    Pytest学习笔记 记录下pytest官方文档的阅读笔记,以便后续参考使用。非完整版,个人理解为主,难免有误,望...

  • python中的yield

    第一次看到yield是在python学习手册上,在python表达式操作符这一节:操作符 :yield x 描述...

  • yield* 学习笔记

    yield*expression用于委托给另一个generator或可迭代对象 语法 expression:返回一...

  • pytest学习笔记

    一、介绍 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1、简单灵活,容易上手;...

  • pytest学习笔记

    使用环境 python3 pycharm pytest pytest编写规范 测试文件以test_开头或以_tes...

  • yield from学习笔记

    用法一: 运行结果: yield from的主要功能是打开双向通道,把最外层的调用方与最内层的子生成器连接起来,这...

  • pytest框架学习-基础入门

    查看所有Python相关学习笔记 pytest学习 一、相关内容的下载安装 1.1 基础安装 1.2 html报告...

  • pytest学习笔记--(一)

    这两天在学习pytest,之前有小用到pytest,觉得这个测试框架很灵巧,用在实现接口自动化(pytest+re...

网友评论

    本文标题:学习笔记-Pytest(五)yield操作

    本文链接:https://www.haomeiwen.com/subject/qsrdsqtx.html