学习-Pytest(五)yield操做

1.fixture的teardown操做并非独立的函数,用yield关键字呼唤teardown操做

2.scope="module"

1.fixture参数scope=”module”,module做用是整个.py文件都会生效( 整个文件只会执行一次),python

用例调用时,参数写上函数名称就行浏览器

# 新建一个文件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"])

运行结果:session

============================= 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_f1.py 打开浏览器,而且打开百度首页
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
.

========================== 3 passed in 0.01 seconds ===========================
View Code

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

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"])

运行结果:spa

============================= 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_f1.py 用例1:搜索python-1
.打开浏览器,而且打开百度首页
用例2:搜索python-2
.用例3:搜索python-3
.

========================== 3 passed in 0.01 seconds ===========================
View Code

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

3. yield执行teardown

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

# 新建一个文件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"])

运行结果:blog

============================= 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_f1.py 打开浏览器,而且打开百度首页
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
.执行teardown!
最后关闭浏览器

========================== 3 passed in 0.01 seconds ===========================
View Code

3. yield遇到异常

1.若是其中一个用例出现异常,不影响yield后面的teardown执行,运行结果互不影响,而且在用例所有执行完以后,会呼唤teardown的内容utf-8

# 新建一个文件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"])

运行结果:

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!
最后关闭浏览器
View Code

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
相关文章
相关标签/搜索