乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      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))

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

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

      使用場景:正常的測試用例,創(chuàng)建資源后,需要清理環(huán)境再在用例中刪除資源,或者tearDown()后進(jìn)行資源清理,不方便,如果用了addCleanup()后,直接在用例中寫入函數(shù),在tearDown()用例后,會再次調(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é)果如下,可以看到會在用例結(jié)束之后,執(zhí)行addCleanup里的函數(shù),因此可以用來進(jìn)行,測試環(huán)境數(shù)據(jù)清理工作。

      Ran 1 test in 12.008s

      OK

      This is tearDown

      abcdefg123312

      Process finished with exit code 0

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多