<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 15.1.?處理 bugs 盡管你很努力地編寫全面的單元測試,但是 bug 還是會出現。我所說的 “bug” 是什么呢?Bug 是你還沒有編寫的測試用例。 ## 例?15.1.?關于 Bug ``` >>> import roman5 >>> roman5.fromRoman("") 0 ``` | | | | --- | --- | | \[1\] | 在前面的[章節中](../unit_testing/stage_5.html "14.5.?roman.py, 第 5 階段")你注意到一個空字符串會匹配上那個檢查羅馬數字有效性的正則表達式了嗎?對于最終版本中的正則表達式這一點仍然沒有改變。這就是一個 Bug ,你希望空字符串能夠像其他無效的羅馬數字表示一樣引發 `InvalidRomanNumeralError` 異常。 | 在重現這個 Bug 并修改它之前你應該編寫一個會失敗的測試用例來說明它。 ## 例?15.2.?測試 bug (`romantest61.py`) ``` class FromRomanBadInput(unittest.TestCase): # previous test cases omitted for clarity (they haven't changed) def testBlank(self): """fromRoman should fail with blank string""" self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, "") ``` | | | | --- | --- | | \[1\] | 這里很簡單。以空字符串調用 `fromRoman` 并確保它會引發一個 `InvalidRomanNumeralError` 異常。難點在于找出 Bug,既然你已經知道它了,測試就簡單了。 | 因為你的代碼存在一個 Bug,并且你編寫了測試這個 Bug 的測試用例,所以測試用例將會失敗: ## 例?15.3.?用 `romantest61.py` 測試 `roman61.py` 的結果 ``` fromRoman should only accept uppercase input ... ok toRoman should always return uppercase ... ok fromRoman should fail with blank string ... FAIL fromRoman should fail with malformed antecedents ... ok fromRoman should fail with repeated pairs of numerals ... ok fromRoman should fail with too many repeated numerals ... ok fromRoman should give known result with known input ... ok toRoman should give known result with known input ... ok fromRoman(toRoman(n))==n for all n ... ok toRoman should fail with non-integer input ... ok toRoman should fail with negative input ... ok toRoman should fail with large input ... ok toRoman should fail with 0 input ... ok ====================================================================== FAIL: fromRoman should fail with blank string ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage6\romantest61.py", line 137, in testBlank self.assertRaises(roman61.InvalidRomanNumeralError, roman61.fromRoman, "") File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ---------------------------------------------------------------------- Ran 13 tests in 2.864s FAILED (failures=1) ``` _現在_ 你可以修改這個 Bug了。 ## 例?15.4.?修改 Bug (`roman62.py`) 這個文件可以在例子目錄下的 `py/roman/stage6/` 目錄中找到。 ``` def fromRoman(s): """convert Roman numeral to integer""" if not s: raise InvalidRomanNumeralError, 'Input can not be blank' if not re.search(romanNumeralPattern, s): raise InvalidRomanNumeralError, 'Invalid Roman numeral: %s' % s result = 0 index = 0 for numeral, integer in romanNumeralMap: while s[index:index+len(numeral)] == numeral: result += integer index += len(numeral) return result ``` | | | | --- | --- | | \[1\] | 只需要兩行代碼:一行直接檢查空字符串和一行 `raise` 語句。 | ## 例?15.5.?用 `romantest62.py` 測試 `roman62.py` 的結果 ``` fromRoman should only accept uppercase input ... ok toRoman should always return uppercase ... ok fromRoman should fail with blank string ... ok fromRoman should fail with malformed antecedents ... ok fromRoman should fail with repeated pairs of numerals ... ok fromRoman should fail with too many repeated numerals ... ok fromRoman should give known result with known input ... ok toRoman should give known result with known input ... ok fromRoman(toRoman(n))==n for all n ... ok toRoman should fail with non-integer input ... ok toRoman should fail with negative input ... ok toRoman should fail with large input ... ok toRoman should fail with 0 input ... ok ---------------------------------------------------------------------- Ran 13 tests in 2.834s OK ``` | | | | --- | --- | | \[1\] | 空字符串測試用例現在通過了,說明 Bug 被修正了。 | | \[2\] | 所有其他測試用例依然通過,證明這個 Bug 修正沒有影響到其他部分。不需要再編程了。 | 這樣編程,并沒有令 Bug 修正變得簡單。簡單的 Bug (就像這一個) 需要簡單的測試用例,復雜 Bug 則需要復雜的測試用例。以測試為核心的氛圍_好像_ 延長了修正 Bug 的時間,因為你需要先貼切地描述出 Bug (編寫測試用例) 然后才去修正它。如果測試用例沒能正確通過,你需要思量這個修改錯了還是測試用例本身出現了 Bug。無論如何,從長遠上講,這樣在測試代碼和代碼之間的反復是值得的,因為這樣會使 Bug 在第一時間就被修正的可能性大大提高。而且不論如何更改,你都可以輕易地重新運行_所有_ 測試用例,新代碼破壞老代碼的機會也變得微乎其微。今天的單元測試就是明天的回歸測試 (regression test)。
                  <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>

                              哎呀哎呀视频在线观看