午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

unittese 運(yùn)行完清除數(shù)據(jù)

 小豬窩969 2019-12-12
 def addCleanup(self, function, *args, **kwargs):        """Add a function, with arguments, to be called when the test is
        completed. Functions added are called on a LIFO basis and are
        called after tearDown on test failure or success.

        Cleanup items are called even if setUp fails (unlike tearDown)."""
        self._cleanups.append((function, args, kwargs))

添加針對(duì)每個(gè)測(cè)試用例執(zhí)行完tearDown()方法之后的清理方法,添加進(jìn)去的函數(shù)按照LIFO的順序,通過參數(shù)添加進(jìn)去

如果setUp()執(zhí)行失敗,就不會(huì)執(zhí)行tearDown(),自然也不會(huì)執(zhí)行addCleanup()里添加的函數(shù)。

使用場(chǎng)景:正常的測(cè)試用例,創(chuàng)建資源后,需要清理環(huán)境再在用例中刪除資源,或者tearDown()后進(jìn)行資源清理,不方便,如果用了addCleanup()后,直接在用例中寫入函數(shù),在tearDown()用例后,會(huì)再次調(diào)用addCleanup來刪除資源,減少代碼量以及遺漏刪除。


# _*_ encoding:utf-8 _*_

from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
import time

class Baidu(unittest.TestCase):

def abc(self,a,b):
print(a+b)

def setUp(self):
self.driver = webdriver.Ie()
self.driver.implicitly_wait(10)
self.base_url = 'http://www.baidu.com'
print ("This is setUp")

def test_baidu_search(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("HTMLTestRunner")
driver.find_element_by_id("su").click()
cleanups = ('abcdefg','123312')
self.addCleanup(self.abc,
cleanups[0], cleanups[1])

def tearDown(self):
self.driver.quit()
print ("This is tearDown")

if __name__ == '__main__':
unittest.main()

執(zhí)行結(jié)果如下,可以看到會(huì)在用例結(jié)束之后,執(zhí)行addCleanup里的函數(shù),因此可以用來進(jìn)行,測(cè)試環(huán)境數(shù)據(jù)清理工作。

Ran 1 test in 12.008s

OK

This is tearDown

abcdefg123312

Process finished with exit code 0

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多