Python Pytest中fixture之yield唤醒teardown和终结函数addfinalizer

引入
我们之前学习的都是测试用例的前置固件,也就是相当于“setup”。说到这,细心的你可能想到了,那有没有什么方式可以表示出“teardown”?这就是我们今天学习的yield和addfinalizer。

yield
yield是一个关键字,它不是单独存在的,要写在fixtrue标记的固件中。

我们在声明的固件myfixture中加入yield关键字,在它下面写测试用例执行后想要运行的代码;其他有关于固件的使用没有任何差别。需要说明的一点是我们在pytest主函数中增加了一个参数“–setup-show”,他会显示出固件的执行情况。
 

import pytest

@pytest.fixture()
def myfixture():
    print("执行myfixture前半部分")
    yield
    print("执行myfixture的后半部分")

class Test_firstFile():

    def test_one(self,myfixture):
        print("执行test_one")
        assert 1+2==3

    def test_two(self):
        print("执行test_two")
        assert 1==1

    def test_three(self):
        print("执行test_three")
        assert 1+1==2

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


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items

test03.py 执行myfixture前半部分

        SETUP    F myfixture
        test03.py::Test_firstFile::test_one (fixtures used: myfixture)执行test_one
.执行myfixture的后半部分

        TEARDOWN F myfixture
        test03.py::Test_firstFile::test_two执行test_two
.
        test03.py::Test_firstFile::test_three执行test_three
.

============================== 3 passed in 0.03s ==============================

Process finished with exit code 0


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

@pytest.fixture(scope="function",autouse=True)
def open():
    print("打开浏览器,并且打开百度首页")
    yield
    print("执行teardown!")


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

def test_s2():
    print("用例2:搜索python-2")

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


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


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items

test03.py 打开浏览器,并且打开百度首页

        SETUP    F open
        test03.py::test_s1 (fixtures used: open)用例1:搜索python-1 None
.执行teardown!

        TEARDOWN F open打开浏览器,并且打开百度首页

        SETUP    F open
        test03.py::test_s2 (fixtures used: open)用例2:搜索python-2
.执行teardown!

        TEARDOWN F open打开浏览器,并且打开百度首页

        SETUP    F open
        test03.py::test_s3 (fixtures used: open)用例3:搜索python-3
.执行teardown!

        TEARDOWN F open

============================== 3 passed in 0.03s ==============================

Process finished with exit code 0


如果测试用例中的代码出现异常或者断言失败,并不会影响他的固件中yield后的代码执行;但是如果固件中的yield之前的代码也就是相当于setup部分的带代码,出现错误或断言失败,那么yield后的代码将不会再执行,当然测试用例中的代码也不会执行。

@pytest.fixture(scope="function",autouse=True)
def open():
    print("打开浏览器,并且打开百度首页")
    yield
    print("执行teardown!")


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

def test_s2():
    print("用例2:搜索python-2")

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


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


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items

test03.py 打开浏览器,并且打开百度首页

        SETUP    F open
        test03.py::test_s1 (fixtures used: open)F执行teardown!

        TEARDOWN F open打开浏览器,并且打开百度首页

        SETUP    F open
        test03.py::test_s2 (fixtures used: open)用例2:搜索python-2
.执行teardown!

        TEARDOWN F open打开浏览器,并且打开百度首页

        SETUP    F open
        test03.py::test_s3 (fixtures used: open)用例3:搜索python-3
.执行teardown!

        TEARDOWN F open

================================== FAILURES ===================================
___________________________________ test_s1 ___________________________________

open = None

    def test_s1(open):
>       print("用例1:搜索python-1",aaaa)
E       NameError: name 'aaaa' is not defined

test03.py:78: NameError
========================= 1 failed, 2 passed in 0.13s =========================

我们也可以通过request.addfinalizer()的方式实现“teardown”

我们在固件中传入request参数;又在固件中定义了一个内置函数;最后将定义的内置函数添加到request的addfinalizer中

@pytest.fixture()
def myfixture(request):
    print ("执行固件myfixture的前半部分")
    def myteardown():
        print("执行固件myfture的后半部分")
    request.addfinalizer(myteardown)


class Test_Pytest():

        def test_one(self,myfixture):
                print("test_one方法执行" )
                assert 1==1

        def test_two(self):
                print("test_two方法执行" )
                assert "o" in "love"

        def test_three(self):
                print("test_three方法执行" )
                assert 3-2==1

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



"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
collected 3 items

test03.py 执行固件myfixture的前半部分

        SETUP    F myfixture
        test03.py::Test_Pytest::test_one (fixtures used: myfixture)test_one方法执行
.执行固件myfture的后半部分

        TEARDOWN F myfixture
        test03.py::Test_Pytest::test_twotest_two方法执行
.
        test03.py::Test_Pytest::test_threetest_three方法执行
.

============================== 3 passed in 0.04s ==============================

Process finished with exit code 0


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
pytest和unittest都是Python常用的测试框架。其,setup和teardown是两个常用的测试用例执行前和执行后的钩子函数。 在pytest,setup和teardown可以通过pytest.fixture装饰器来使用。装饰器可以被附加到函数、方法或类上,以标记其为fixture。当测试函数需要使用fixture时,它们可以将fixture名称作为输入参数,pytest将自动查找和运行fixture函数,并将其输出值传递给测试函数。例如: ```python import pytest @pytest.fixture def setup(): print("执行setup操作") yield print("执行teardown操作") def test_example(setup): print("执行测试操作") ``` 在这个例子,setup函数被标记为fixture,test_example函数接收setup作为输入参数。在运行test_example函数之前,pytest将自动运行setup函数,然后运行测试函数,最后再运行teardown函数。 在unittest,setup和teardown可以通过setUp和tearDown方法来实现。这些方法被定义在unittest.TestCase类,并在每次运行测试方法之前和之后自动调用。例如: ```python import unittest class TestExample(unittest.TestCase): def setUp(self): print("执行setup操作") def tearDown(self): print("执行teardown操作") def test_example(self): print("执行测试操作") ``` 在这个例子,TestExample类继承自unittest.TestCase类,并覆盖了setUp和tearDown方法。在运行test_example方法之前,unittest将自动调用setUp方法,然后运行测试方法,最后再调用tearDown方法。 总体而言,pytest和unittest都提供了简单易用的setup和teardown机制来帮助测试人员编写可靠的测试用例。但是,pytest相对于unittest更加灵活,可以通过fixture装饰器来定义setup和teardown函数,同时也提供了更多的扩展性和定制化选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大力测试进阶之路

打赏博主喝瓶水吧!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值