<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] 前言 在測試用例中,執行完測試用例后,最后一步是判斷測試結果是pass還是fail,自動化測試腳本里面一般把這種生成測試結果的方法稱為斷言(assert)。 用unittest組件測試用例的時候,斷言的方法還是很多的,下面介紹幾種常用的斷言方法:assertEqual、assertIn、assertTrue. <br /> ## 一、簡單案例 1. 下面寫了4個case,其中第四個是執行失敗的 ``` import unittest class Test(unittest.TestCase): def test01(self): '''判斷 a == b''' a = 1 b = 1 self.assertEqual(a, b) def test02(self): '''判斷a in b''' a = "hello" b = "hello world!" self.assertIn(a, b) def test03(self): '''判斷 a is Ture''' a = True self.assertTrue(a) def test04(self): a = "上海-悠悠" b = "yoyo" self.assertEqual(a, b, msg="失敗原因:%s != %s" %(a, b)) if __name__ == "__main__": unittest.main() ``` 2. 執行結果如下 ``` Failure Expected :'\xe4\xb8\x8a\xe6\xb5\xb7-\xe6\x82\xa0\xe6\x82\xa0' Actual :'yoyo' <Click to see difference> Traceback (most recent call last): File "D:\test\yoyotest\kecheng\test12.py", line 27, in test04 self.assertEqual(a, b) AssertionError: '\xe4\xb8\x8a\xe6\xb5\xb7-\xe6\x82\xa0\xe6\x82\xa0' != 'yoyo' ``` 3. 執行的結果,中文編碼不對,沒正常顯示中文,遇到這種情況,可以自定義異常輸出 <br /> ## 二、自定義異常 1. 以assertEqual為例分析: ``` assertEqual(self, first, second, msg=None) ??? Fail if the two objects are unequal as determined by the '==' ??? operator. ``` 2. 翻譯:如果兩個對象不能相等,就返回失敗,相當于return: first==second 3. 這里除了相比較的兩個參數first和second,還有第三個參數msg=None,這個msg參數就是遇到異常后自定義輸出信息 ![Snipaste_2020-09-11_17-58-59.png](http://i.loli.net/2020/09/11/SQP1h5ewCLmJEXI.png) <br /> ## 三、unittest常用的斷言方法 ``` 1.assertEqual(self, first, second, msg=None) --判斷兩個參數相等:first == second 2.assertNotEqual(self, first, second, msg=None) --判斷兩個參數不相等:first != second 3.assertIn(self, member, container, msg=None) --判斷是字符串是否包含:member in container 4.assertNotIn(self, member, container, msg=None) --判斷是字符串是否不包含:member not in container 5.assertTrue(self, expr, msg=None) --判斷是否為真:expr is True 6.assertFalse(self, expr, msg=None) --判斷是否為假:expr is False 7.assertIsNone(self, obj, msg=None) --判斷是否為None:obj is None 8.assertIsNotNone(self, obj, msg=None) --判斷是否不為None:obj is not None ```
                  <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>

                              哎呀哎呀视频在线观看