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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 14.1.?`roman.py`, 第 1 階段 到目前為止,單元測試已經完成,是時候開始編寫被單元測試測試的代碼了。你將分階段地完成這個工作,因此開始時所有的單元測試都是失敗的,但在逐步完成 `roman.py` 的同時你會看到它們一個個地通過測試。 ## 例?14.1.?`roman1.py` 這個程序可以在例子目錄下的 `py/roman/stage1/` 目錄中找到。 如果您還沒有下載本書附帶的樣例程序, 可以 [下載本程序和其他樣例程序](http://www.woodpecker.org.cn/diveintopython/download/diveintopython-exampleszh-cn-5.4b.zip "Download example scripts")。 ``` """Convert to and from Roman numerals""" #Define exceptions class RomanError(Exception): pass class OutOfRangeError(RomanError): pass class NotIntegerError(RomanError): pass class InvalidRomanNumeralError(RomanError): pass def toRoman(n): """convert integer to Roman numeral""" pass def fromRoman(s): """convert Roman numeral to integer""" pass ``` | | | | --- | --- | | \[1\] | 這就是如何定義你自己的 Python 異常。異常 (Exception) 也是類,通過繼承已有的異常,你可以創建自定義的異常。強烈建議 (但不是必須) 你繼承 `Exception` 來定義自己的異常,因為它是所有內建異常的基類。這里我定義了 `RomanError` (從 `Exception` 繼承而來) 作為我所有自定義異常的基類。這是一個風格問題,我也可以直接從 `Exception` 繼承建立每一個自定義異常。 | | \[2\] | `OutOfRangeError` 和 `NotIntegerError` 異常將會最終被用于 `toRoman` 以標示不同類型的無效輸入,更具體而言就是 [`ToRomanBadInput`](testing_for_failure.html#roman.tobadinput.example "例?13.3.?測試 toRoman 的無效輸入") 測試的那些。 | | \[3\] | `InvalidRomanNumeralError` 將被最終用于 `fromRoman` 以標示無效輸入,具體而言就是 [`FromRomanBadInput`](testing_for_failure.html#roman.frombadinput.example "例?13.4.?測試 fromRoman 的無效輸入")測試的那些。 | | \[4\] | 在這一步中你只是想定義每個函數的 API ,而不想具體實現它們,因此你以 Python 關鍵字 [`pass`](../object_oriented_framework/defining_classes.html#fileinfo.class.simplest "例?5.3.?最簡單的 Python 類") 姑且帶過。 | 重要的時刻到了 (請打起鼓來):你終于要對這個簡陋的小模塊開始運行單元測試了。目前而言,每一個測試用例都應該失敗。事實上,任何測試用例在此時通過,你都應該回頭看看 `romantest.py` ,仔細想想為什么你寫的測試代碼如此沒用,以至于連什么都不作的函數都能通過測試。 用命令行選項 `-v` 運行 `romantest1.py` 可以得到更詳細的輸出信息,這樣你就可以看到每一個測試用例的具體運行情況。如果幸運,你的結果應該是這樣的: ## 例?14.2.?以 `romantest1.py` 測試 `roman1.py` 的輸出 ``` fromRoman should only accept uppercase input ... ERROR toRoman should always return uppercase ... ERROR fromRoman should fail with malformed antecedents ... FAIL fromRoman should fail with repeated pairs of numerals ... FAIL fromRoman should fail with too many repeated numerals ... FAIL fromRoman should give known result with known input ... FAIL toRoman should give known result with known input ... FAIL fromRoman(toRoman(n))==n for all n ... FAIL toRoman should fail with non-integer input ... FAIL toRoman should fail with negative input ... FAIL toRoman should fail with large input ... FAIL toRoman should fail with 0 input ... FAIL ====================================================================== ERROR: fromRoman should only accept uppercase input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 154, in testFromRomanCase roman1.fromRoman(numeral.upper()) AttributeError: 'None' object has no attribute 'upper' ====================================================================== ERROR: toRoman should always return uppercase ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 148, in testToRomanCase self.assertEqual(numeral, numeral.upper()) AttributeError: 'None' object has no attribute 'upper' ====================================================================== FAIL: fromRoman should fail with malformed antecedents ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 133, in testMalformedAntecedent self.assertRaises(roman1.InvalidRomanNumeralError, roman1.fromRoman, s) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ====================================================================== FAIL: fromRoman should fail with repeated pairs of numerals ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 127, in testRepeatedPairs self.assertRaises(roman1.InvalidRomanNumeralError, roman1.fromRoman, s) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ====================================================================== FAIL: fromRoman should fail with too many repeated numerals ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 122, in testTooManyRepeatedNumerals self.assertRaises(roman1.InvalidRomanNumeralError, roman1.fromRoman, s) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ====================================================================== FAIL: fromRoman should give known result with known input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 99, in testFromRomanKnownValues self.assertEqual(integer, result) File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual raise self.failureException, (msg or '%s != %s' % (first, second)) AssertionError: 1 != None ====================================================================== FAIL: toRoman should give known result with known input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 93, in testToRomanKnownValues self.assertEqual(numeral, result) File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual raise self.failureException, (msg or '%s != %s' % (first, second)) AssertionError: I != None ====================================================================== FAIL: fromRoman(toRoman(n))==n for all n ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 141, in testSanity self.assertEqual(integer, result) File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual raise self.failureException, (msg or '%s != %s' % (first, second)) AssertionError: 1 != None ====================================================================== FAIL: toRoman should fail with non-integer input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 116, in testNonInteger self.assertRaises(roman1.NotIntegerError, roman1.toRoman, 0.5) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: NotIntegerError ====================================================================== FAIL: toRoman should fail with negative input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 112, in testNegative self.assertRaises(roman1.OutOfRangeError, roman1.toRoman, -1) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: OutOfRangeError ====================================================================== FAIL: toRoman should fail with large input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 104, in testTooLarge self.assertRaises(roman1.OutOfRangeError, roman1.toRoman, 4000) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: OutOfRangeError ====================================================================== FAIL: toRoman should fail with 0 input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage1\romantest1.py", line 108, in testZero self.assertRaises(roman1.OutOfRangeError, roman1.toRoman, 0) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: OutOfRangeError ---------------------------------------------------------------------- Ran 12 tests in 0.040s FAILED (failures=10, errors=2) ``` | | | | --- | --- | | \[1\] | 運行腳本將會執行 `unittest.main()`,由它來執行每個測試用例,也就是每個在 `romantest.py` 中定義的方法。對于每個測試用例,無論測試通過與否,都會輸出這個方法的 `doc string`。意料之中,沒有通過一個測試用例。 | | \[2\] | 對于每個失敗的測試用例,`unittest` 顯示的跟蹤信息告訴我們都發生了什么。就此處而言,調用 `assertRaises` (也稱作 `failUnlessRaises`) 引發了一個 `AssertionError` 異常,因為期待 `toRoman` 所引發的 `OutOfRangeError` 異常沒有出現。 | | \[3\] | 在這些細節后面,`unittest` 給出了一個關于被執行測試的個數和花費時間的總結。 | | \[4\] | 總而言之,由于至少一個測試用例沒有通過,單元測試失敗了。當某個測試用例沒能通過時,`unittest` 會區分是失敗 (failures) 還是錯誤 (errors)。失敗是指調用 `assertXYZ` 方法,比如 `assertEqual` 或者 `assertRaises` 時,斷言的情況沒有發生或預期的異常沒有被引發。而錯誤是指你測試的代碼或單元測試本身發生了某種異常。例如:`testFromRomanCase` 方法 (“`fromRoman` 只接受大寫輸入”) 就是一個錯誤,因為調用 `numeral.upper()` 引發了一個 `AttributeError` 異常,因為 `toRoman` 的返回值不是期望的字符串類型。但是,`testZero` (“`toRoman` 應該在輸入 0 時失敗”) 是一個失敗,因為調用 `fromRoman` 沒有引發一個 `assertRaises` 期待的異常:`InvalidRomanNumeral`。 |
                  <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>

                              哎呀哎呀视频在线观看