<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] 前言 熟悉java的應該都清楚常見的單元測試框架Junit和TestNG,這個招聘的需求上也是經常見到的。python里面也有單元測試框架-unittest,相當于是一個python版的junit。 python里面的單元測試框架除了unittest,還有一個pytest框架。 <br /> ## 一、unittest簡介 ``` 1. 先導入unittest 2. 用help函數查看源碼解析 3. 查看描述: Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's Smalltalk testing framework. 翻譯:python的單元測試框架,是基于java的junit測試框架 ``` ![](http://images2015.cnblogs.com/blog/1070438/201704/1070438-20170404201834113-1812106740.png) <br /> ## 二、簡單用法 1.可以把上圖的這段代碼copy出來,單獨運行,看看測試結果。 Simple usage: import unittest class IntegerArithmeticTestCase(unittest.TestCase): def testAdd(self): ## test method names begin 'test*' self.assertEqual((1 + 2), 3) self.assertEqual(0 + 1, 1) def testMultiply(self): self.assertEqual((0 * 10), 0) self.assertEqual((5 * 8), 40) if __name__ == '__main__': unittest.main() 2. 第一行是導入unittest這個模塊 3. class這一行是定義一個測試的類,并繼承unittest.TestCase這個類 4. 接下來是定義了兩個測試case名稱:testAdd和testMultiply 5. 注釋里面有句話很重要,這個要敲下黑板記筆記了:## test method names begin 'test*' --翻譯:**測試用例的名稱要以test開頭** 6. 然后是斷言assert,這里的斷言方法是assertEqual-判斷兩個是否相等,這個斷言可以是一個也可以是多個 7. if下面的這個unittest.main()是運行主函數,運行后會看到測試結果(跑了兩個用例耗時0.000秒,兩個用例都通過): ``` .. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK ``` <br /> ## 三、小試牛刀 1. 上面的兩個案例是加法和乘法,我們可以寫個case試下減法和除法。 2. 有很多小伙伴不知道斷言怎么寫,斷言其實就是拿實際結果和期望結果去對比,對比的方法很多,這里只是舉的最簡單的一個判斷相等的方法。 ``` import unittest class Test(unittest.TestCase): def testMinus(self): # test method names begin 'test*' '''測試減法''' result = 100 - 10 # 實際結果 hope = 90 # 期望結果 self.assertEqual(result, hope) def testDivide(self): '''測試除法''' result = 7/2 # 實際結果 hope = 3.5 # 期望結果 self.assertEqual(result, hope) if __name__ == '__main__': unittest.main() ``` <br /> ## 四、前置和后置 1. setUp:在寫測試用例的時候,每次操作其實都是基于打開瀏覽器輸入對應網址這些操作,這個就是執行用例的前置條件。 2. tearDown:執行完用例后,為了不影響下一次用例的執行,一般有個數據還原的過程,這就是執行用例的后置條件。 3. 很多人執行完用例,都不去做數據還原,以致于下一個用例執行失敗,這就是不喜歡擦屁股的事情,習慣不好。 4. 前置和后置都是非必要的條件,如果沒有也可以寫pass ``` import unittest class Test(unittest.TestCase): def setUp(self): pass # 如果沒有可以不寫,或者pass代替 def tearDown(self): pass ``` <br /> ## 五、博客案例 1. 打開博客首頁為例,寫一個簡單的case 2. 判斷title完全等于期望結果 3. 運行通過,下面會有一個綠條顯示:1 test passed ![Snipaste_2020-09-10_16-58-53.png](http://i.loli.net/2020/09/10/N6fubYaVI7UJP1W.png) <br /> 六、參考代碼 ``` from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from time import sleep import unittest class Blog(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get("https://home.cnblogs.com/zjut-cheng") def test_blog(self): sleep(3) result = EC.title_is("梔楠的主頁 - 博客園")(self.driver) print(result) self.assertTrue(result) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main() ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看