<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 13.5.?負面測試 (Testing for failure) 使用有效輸入確保函數成功通過測試還不夠,你還需要測試無效輸入導致函數失敗的情形。但并不是任何失敗都可以,必須如你預期地失敗。 還記得 `toRoman` 的[其他要求](index.html#roman.requirements)吧: 1. `toRoman` 在輸入值為 `1` 到 `3999` 之外時失敗。 2. `toRoman` 在輸入值為非整數時失敗。 在 Python 中,函數以引發[異常](../file_handling/index.html#fileinfo.exception "6.1.?異常處理")的方式表示失敗。`unittest` 模塊提供了用于測試函數是否在給定無效輸入時引發特定異常的方法。 ## 例?13.3.?測試 `toRoman` 的無效輸入 ``` class ToRomanBadInput(unittest.TestCase): def testTooLarge(self): """toRoman should fail with large input""" self.assertRaises(roman.OutOfRangeError, roman.toRoman, 4000) def testZero(self): """toRoman should fail with 0 input""" self.assertRaises(roman.OutOfRangeError, roman.toRoman, 0) def testNegative(self): """toRoman should fail with negative input""" self.assertRaises(roman.OutOfRangeError, roman.toRoman, -1) def testNonInteger(self): """toRoman should fail with non-integer input""" self.assertRaises(roman.NotIntegerError, roman.toRoman, 0.5) ``` | | | | --- | --- | | \[1\] | `unittest` 模塊中的 `TestCase` 類提供了 `assertRaises` 方法,它接受這幾個參數:預期的異常、測試的函數,以及傳遞給函數的參數。(如果被測試函數有不止一個參數,把它們按順序全部傳遞給 `assertRaises` ,它會把這些參數傳給被測的函數。) 特別注意這里的操作:不是直接調用 `toRoman` 再手工查看是否引發特定異常 (使用 [`try...except` 塊](../file_handling/index.html#fileinfo.exception "6.1.?異常處理")捕捉異常),`assertRaises` 為我們封裝了這些。所有你要做的就是把異常 (`roman.OutOfRangeError`)、函數 (`toRoman`) 以及 `toRoman` 的參數 (`4000`) 傳遞給 `assertRaises` ,它會調用 `toRoman` 查看是否引發 `roman.OutOfRangeError` 異常。(還應注意到你是把 `toRoman` 函數本身當作一個參數,而不是調用它,傳遞它的時候也不是把它的名字作為一個字符串。我提到過嗎?無論是函數還是異常, [Python 中萬物皆對象](../getting_to_know_python/everything_is_an_object.html "2.4.?萬物皆對象"))。 | | \[2\] | 與測試過大的數相伴的便是測試過小的數。記住,羅馬數字不能表示 `0` 和負數,所以你要分別編寫測試用例 ( `testZero` 和 `testNegative`)。在 `testZero` 中,你測試 `toRoman` 調用 `0` 引發的 `roman.OutOfRangeError` 異常,如果_沒能_ 引發 `roman.OutOfRangeError` (不論是返回了一個值還是引發了其他異常),則測試失敗。 | | \[3\] | [要求 #3](index.html#roman.requirements):`toRoman` 不能接受非整數輸入,所以這里你測試 `toRoman` 在輸入 `0.5` 時引發 `roman.NotIntegerError` 異常。如果 `toRoman` 沒有引發 `roman.NotIntegerError` 異常,則測試失敗。 | 接下來的兩個[要求](index.html#roman.requirements)與前三個類似,不同點是他們所針對的是 `fromRoman` 而不是 `toRoman`: 1. `fromRoman` 應該能將輸入的有效羅馬數字轉換為相應的阿拉伯數字表示。 2. `fromRoman` 在輸入無效羅馬數字時應該失敗。 要求 #4 與[要求 #1](testing_for_success.html#roman.testtoromanknownvalues.example "例?13.2.?testToRomanKnownValues") 的處理方法相同,即測試一個已知樣本中的一個個數字對。要求 #5 與 #2 和 #3的處理方法相同,即通過無效輸入確認 `fromRoman` 引發恰當的異常。 ## 例?13.4.?測試 `fromRoman` 的無效輸入 ``` class FromRomanBadInput(unittest.TestCase): def testTooManyRepeatedNumerals(self): """fromRoman should fail with too many repeated numerals""" for s in ('MMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'): self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s) def testRepeatedPairs(self): """fromRoman should fail with repeated pairs of numerals""" for s in ('CMCM', 'CDCD', 'XCXC', 'XLXL', 'IXIX', 'IVIV'): self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s) def testMalformedAntecedent(self): """fromRoman should fail with malformed antecedents""" for s in ('IIMXCC', 'VX', 'DCM', 'CMM', 'IXIV', 'MCMC', 'XCX', 'IVI', 'LM', 'LD', 'LC'): self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s) ``` | | | | --- | --- | | \[1\] | 沒什么新鮮的,與測試 `toRoman` 無效輸入時相同的模式,只是你有了一個新的異常:`roman.InvalidRomanNumeralError`。`roman.py` 中一共要定義三個異常 (另外的兩個是 `roman.OutOfRangeError` 和 `roman.NotIntegerError`)。稍后你在開始編寫 `roman.py` 時將會知道如何定義這些異常。 |
                  <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>

                              哎呀哎呀视频在线观看