前言

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

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

scope="module"

1.fixture参数scope="module",module作用是整个.py文件都会生效,用例调用时,参数写上函数名称就行。

# coding:utf-8
#test_fix1.py
import pytest
@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并且打开百度")
def test_1(open):
    print("test_1:搜索fix1")
def test_2(open):
    print("test_2:搜索fix2")
def test_3(open):
    print("test_3:搜索fix3")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

运行结果:

pytest--fixture之yield实现teardown_百度

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

2.如果test_1不调用,test_2(调用open),test_3不调用,运行顺序会是怎样的?

# coding:utf-8
#test_fix1.py
import pytest
@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并且打开百度")
def test_1():#不调用open
    print("test_1:搜索fix1")
def test_2(open):
    print("test_2:搜索fix2")
def test_3(open):
    print("test_3:搜索fix3")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"]) 

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py 打开浏览器,并且打开百度
.test_1:搜索fix1
.test_2:搜索fix2
.test_3:搜索fix3
                                                          [100%]

============================== 3 passed in 0.11s ==============================

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

yield执行teardown

1.前面讲的是在用例前加前置条件,相当于setup,既然有setup那就有teardown,fixture里面的teardown用yield来唤醒teardown的执行。

# coding:utf-8
#test_fix1.py
import pytest
@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并且打开百度")
    yield
    print("执行teardown")
    print("最后关闭浏览器")
def test_1(open):
    print("test_1:搜索fix1")
def test_2():
    print("test_2:搜索fix2")
def test_3():
    print("test_3:搜索fix3")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py 打开浏览器,并且打开百度
.test_1:搜索fix1
.test_2:搜索fix2
.test_3:搜索fix3
执行teardown
最后关闭浏览器
                                                          [100%]

============================== 3 passed in 0.12s ==============================


yield遇到异常

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

# coding:utf-8
#test_fix1.py
import pytest
@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并且打开百度")
    yield
    print("执行teardown")
    print("最后关闭浏览器")
def test_1(open):
    print("test_1:搜索fix1")
    raise NameError
def test_2():
    print("test_2:搜索fix2")
def test_3():
    print("test_3:搜索fix3")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"]) 

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py 打开浏览器,并且打开百度
Ftest_1:搜索fix1

test_001.py:8 (test_1)
open = None

    def test_1(open):
        print("test_1:搜索fix1")
>       raise NameError
E       NameError

test_001.py:11: NameError
.test_2:搜索fix2
.test_3:搜索fix3
执行teardown
最后关闭浏览器
                                                          [100%]

================================== FAILURES ===================================
___________________________________ test_1 ____________________________________

open = None

    def test_1(open):
        print("test_1:搜索fix1")
>       raise NameError
E       NameError

test_001.py:11: NameError
---------------------------- Captured stdout setup ----------------------------
打开浏览器,并且打开百度
---------------------------- Captured stdout call -----------------------------
test_1:搜索fix1
=========================== short test summary info ===========================
FAILED test_001.py::test_1 - NameError
========================= 1 failed, 2 passed in 0.35s =========================

2.如果在setup就异常了,那么是不会去执行yield后面的teardown内容了

# coding:utf-8
#test_fix1.py
import pytest
@pytest.fixture(scope="module")
def open():
    print("打开浏览器,并且打开百度")
    raise NameError
    yield
    print("执行teardown")
    print("最后关闭浏览器")
def test_1(open):
    print("test_1:搜索fix1")
def test_2():
    print("test_2:搜索fix2")
def test_3():
    print("test_3:搜索fix3")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"]) 

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py E打开浏览器,并且打开百度

test setup failed
@pytest.fixture(scope="module")
    def open():
        print("打开浏览器,并且打开百度")
>       raise NameError
E       NameError

test_001.py:6: NameError
.test_2:搜索fix2
.test_3:搜索fix3
                                                          [100%]

=================================== ERRORS ====================================
__________________________ ERROR at setup of test_1 ___________________________

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

test_001.py:6: NameError
---------------------------- Captured stdout setup ----------------------------
打开浏览器,并且打开百度
=========================== short test summary info ===========================
ERROR test_001.py::test_1 - NameError
========================= 2 passed, 1 error in 0.25s ==========================


addfinalizer终结函数

1.除了yield可以实现teardown,在request-context对象中注册addfinalizer方法也可以实现终结函数。

pytest--fixture之yield实现teardown_搜索_02

2.yield和addfinalizer方法都是在测试完成后呼叫相应的代码。但是addfinalizer不同的是:

  • 他可以注册多个终结函数。
  • 这些终结方法总是会被执行,无论在之前的setup code有没有抛出错误。这个方法对于正确关闭所有的fixture创建的资源非常便利,即使其一在创建或获取时失败