python-unittest(9)

來源:互聯網
上載者:User

測試邊界條件

Testing the edges
When we write automated tests, we pick the inputs and assert the expected outputs. It is
important to test the limits of the inputs to make sure our code can handle good and bad
inputs. This is also known as testing corner cases.

1. Create a new file named recipe9.py in which to put all our code for this recipe.

2. Pick a class to test. In this recipe, we'll use another variation of our Roman numeral
converter. This one doesn't process values greater than 4000.

3. Create a test case that sets up an instance of the Roman numeral converter.

4. Add several test methods that exercise the edges of converting to Roman numeral
notation.

5. Add several test methods that exercise the edges of converting to decimal notation.

6. Add some tests that exercise the tiers of converting decimals to Roman numerals.

7. Add some tests that input unexpected values to the Roman numeral converter.

8. Add a unit test runner.

測試代碼:

Code# !usr/bin/env python 2.7# coding: utf-8# filename: recipe9.pyclass RomanNumeralConverter(object):    def __init__(self):        self.digit_map = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1}    def convert_to_decimal(self, roman_numeral):        val = 0        for char in roman_numeral:            val += self.digit_map[char]        if val > 4000:            raise Exception("We don't handle values over 4000")        return val    def convert_to_roman(self, decimal):        if decimal > 4000:            raise Exception("We don't handle values over 4000")        val = ""        mappers = [(1000,"M"), (500,"D"), (100,"C"), (50,"L"), (10,"X"), (5,"V"), (1,"I")]        for (mapper_dec, mapper_rom) in mappers:            while decimal >= mapper_dec:                val += mapper_rom                decimal -= mapper_dec        return valimport unittestclass RomanNumeralTest(unittest.TestCase):    def setUp(self):        self.cvt = RomanNumeralConverter()    def test_to_roman_bottom(self):        self.assertEquals("I", self.cvt.convert_to_roman(1))    def test_to_roman_below_bottom(self):        self.assertEquals("", self.cvt.convert_to_roman(0))    def test_to_roman_negative_value(self):        self.assertEquals("", self.cvt.convert_to_roman(-1))    def test_to_roman_top(self):        self.assertEquals("MMMM", \                          self.cvt.convert_to_roman(4000))    def test_to_roman_above_top(self):        self.assertRaises(Exception, \                          self.cvt.convert_to_roman, 4001)    def test_to_decimal_bottom(self):        self.assertEquals(1, self.cvt.convert_to_decimal("I"))    def test_to_decimal_below_bottom(self):        self.assertEquals(0, self.cvt.convert_to_decimal(""))    def test_to_decimal_top(self):        self.assertEquals(4000, \                          self.cvt.convert_to_decimal("MMMM"))    def test_to_decimal_above_top(self):        self.assertRaises(Exception, \                         self.cvt.convert_to_decimal, "MMMMI")    def test_to_roman_tier1(self):        self.assertEquals("V", self.cvt.convert_to_roman(5))    def test_to_roman_tier2(self):        self.assertEquals("X", self.cvt.convert_to_roman(10))    def test_to_roman_tier3(self):        self.assertEquals("L", self.cvt.convert_to_roman(50))    def test_to_roman_tier4(self):        self.assertEquals("C", self.cvt.convert_to_roman(100))    def test_to_roman_tier5(self):        self.assertEquals("D", self.cvt.convert_to_roman(500))    def test_to_roman_tier6(self):        self.assertEquals("M", \                          self.cvt.convert_to_roman(1000))    def test_to_roman_bad_inputs(self):        self.assertEquals("", self.cvt.convert_to_roman(None))        self.assertEquals("I", self.cvt.convert_to_roman(1.2))    def test_to_decimal_bad_inputs(self):        self.assertRaises(TypeError, \                          self.cvt.convert_to_decimal, None)        self.assertRaises(TypeError, \                          self.cvt.convert_to_decimal, 1.2)if __name__ == "__main__":    unittest.main()



結果輸出:

.................
----------------------------------------------------------------------
Ran 17 tests in 0.000s

OK

注意類似這樣的代碼:

Code        def test_to_roman_above_top(self):        self.assertRaises(Exception, \                          self.cvt.convert_to_roman, 4001)

 

用於捕獲程式中的異常

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.