日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2025/3/15 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python Pytest中fixture之yield唤醒teardown和终结函数addfinalizer 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

引入
我們之前學(xué)習(xí)的都是測(cè)試用例的前置固件,也就是相當(dāng)于“setup”。說(shuō)到這,細(xì)心的你可能想到了,那有沒(méi)有什么方式可以表示出“teardown”?這就是我們今天學(xué)習(xí)的yield和addfinalizer。

yield
yield是一個(gè)關(guān)鍵字,它不是單獨(dú)存在的,要寫在fixtrue標(biāo)記的固件中。

我們?cè)诼暶鞯墓碳yfixture中加入yield關(guān)鍵字,在它下面寫測(cè)試用例執(zhí)行后想要運(yùn)行的代碼;其他有關(guān)于固件的使用沒(méi)有任何差別。需要說(shuō)明的一點(diǎn)是我們?cè)趐ytest主函數(shù)中增加了一個(gè)參數(shù)“–setup-show”,他會(huì)顯示出固件的執(zhí)行情況。
?

import pytest@pytest.fixture() def myfixture():print("執(zhí)行myfixture前半部分")yieldprint("執(zhí)行myfixture的后半部分")class Test_firstFile():def test_one(self,myfixture):print("執(zhí)行test_one")assert 1+2==3def test_two(self):print("執(zhí)行test_two")assert 1==1def test_three(self):print("執(zhí)行test_three")assert 1+1==2if __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 itemstest03.py 執(zhí)行myfixture前半部分SETUP F myfixturetest03.py::Test_firstFile::test_one (fixtures used: myfixture)執(zhí)行test_one .執(zhí)行myfixture的后半部分TEARDOWN F myfixturetest03.py::Test_firstFile::test_two執(zhí)行test_two .test03.py::Test_firstFile::test_three執(zhí)行test_three .============================== 3 passed in 0.03s ==============================Process finished with exit code 0

fixture里面的teardown用yield來(lái)喚醒teardown的執(zhí)行

@pytest.fixture(scope="function",autouse=True) def open():print("打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)")yieldprint("執(zhí)行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 itemstest03.py 打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)SETUP F opentest03.py::test_s1 (fixtures used: open)用例1:搜索python-1 None .執(zhí)行teardown!TEARDOWN F open打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)SETUP F opentest03.py::test_s2 (fixtures used: open)用例2:搜索python-2 .執(zhí)行teardown!TEARDOWN F open打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)SETUP F opentest03.py::test_s3 (fixtures used: open)用例3:搜索python-3 .執(zhí)行teardown!TEARDOWN F open============================== 3 passed in 0.03s ==============================Process finished with exit code 0

如果測(cè)試用例中的代碼出現(xiàn)異常或者斷言失敗,并不會(huì)影響他的固件中yield后的代碼執(zhí)行;但是如果固件中的yield之前的代碼也就是相當(dāng)于setup部分的帶代碼,出現(xiàn)錯(cuò)誤或斷言失敗,那么yield后的代碼將不會(huì)再執(zhí)行,當(dāng)然測(cè)試用例中的代碼也不會(huì)執(zhí)行。

@pytest.fixture(scope="function",autouse=True) def open():print("打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)")yieldprint("執(zhí)行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 itemstest03.py 打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)SETUP F opentest03.py::test_s1 (fixtures used: open)F執(zhí)行teardown!TEARDOWN F open打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)SETUP F opentest03.py::test_s2 (fixtures used: open)用例2:搜索python-2 .執(zhí)行teardown!TEARDOWN F open打開(kāi)瀏覽器,并且打開(kāi)百度首頁(yè)SETUP F opentest03.py::test_s3 (fixtures used: open)用例3:搜索python-3 .執(zhí)行teardown!TEARDOWN F open================================== FAILURES =================================== ___________________________________ test_s1 ___________________________________open = Nonedef test_s1(open): > print("用例1:搜索python-1",aaaa) E NameError: name 'aaaa' is not definedtest03.py:78: NameError ========================= 1 failed, 2 passed in 0.13s =========================

我們也可以通過(guò)request.addfinalizer()的方式實(shí)現(xiàn)“teardown”

我們?cè)诠碳袀魅雛equest參數(shù);又在固件中定義了一個(gè)內(nèi)置函數(shù);最后將定義的內(nèi)置函數(shù)添加到request的addfinalizer中

@pytest.fixture() def myfixture(request):print ("執(zhí)行固件myfixture的前半部分")def myteardown():print("執(zhí)行固件myfture的后半部分")request.addfinalizer(myteardown)class Test_Pytest():def test_one(self,myfixture):print("test_one方法執(zhí)行" )assert 1==1def test_two(self):print("test_two方法執(zhí)行" )assert "o" in "love"def test_three(self):print("test_three方法執(zhí)行" )assert 3-2==1if __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 itemstest03.py 執(zhí)行固件myfixture的前半部分SETUP F myfixturetest03.py::Test_Pytest::test_one (fixtures used: myfixture)test_one方法執(zhí)行 .執(zhí)行固件myfture的后半部分TEARDOWN F myfixturetest03.py::Test_Pytest::test_twotest_two方法執(zhí)行 .test03.py::Test_Pytest::test_threetest_three方法執(zhí)行 .============================== 3 passed in 0.04s ==============================Process finished with exit code 0

?

總結(jié)

以上是生活随笔為你收集整理的Python Pytest中fixture之yield唤醒teardown和终结函数addfinalizer的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。