溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中setup和teardown怎么用

發(fā)布時(shí)間:2021-09-07 10:48:52 來源:億速云 閱讀:282 作者:小新 欄目:編程語言

小編給大家分享一下python中setup和teardown怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

說明

1、setup/teardown:每種方法將執(zhí)行一次,無論是類內(nèi)還是類外。

2、Setup:方法運(yùn)行前執(zhí)行,表示前置條件。

必須在每個(gè)用例執(zhí)行前執(zhí)行一次。

3、Teardown:方法運(yùn)行后才能執(zhí)行,表示資源釋放。

每次用例執(zhí)行后都會(huì)執(zhí)行一次。

實(shí)例

# file_name: test_setup.py
import pytest
def setup():
    print("...類外setup...")
def test_create():
    print("類外test_create")
def test_view():
    print("類外test_view")
class TestSetupClass:
    def setup(self):
        print("...類內(nèi)setup...")
    def test_create(self):
        print("類內(nèi)test_create")
    def test_view(self):
        print("類內(nèi)test_view")
    def teardown(self):
        print("...類內(nèi)teardown...")
def teardown():
    print("...類外teardown...")
if __name__ == '__main__':
    pytest.main(['-vs', 'test_setup.py'])

看完了這篇文章,相信你對“python中setup和teardown怎么用”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI