Python Pytest中fixture之yield唤醒teardown和终结函数addfinalizer
引入
我們之前學(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í)行情況。
?
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)題。
- 上一篇: oracle 查询cpu 100%,Or
- 下一篇: python 将图片与字符串相互转换