六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 211|回复: 0

python-unittest(9)

[复制链接]

升级  33.33%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
100
 楼主| 发表于 2012-12-17 21:52:34 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body">测试边界条件
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.
测试代码:
<div class="cnblogs_code">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()
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表