Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19698355
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2011-06-15 12:15:30

2011-06-15 磁针石

#
承接软件自动化实施与培训等gtalk ouyangchongwu#gmail.com qq 37391319 博客:testing.blog.chinaunix.net

#版权所有,转载刊登请来函联系

#自动化测试和python群组:

#python qq group: 深圳自动化测试python群:113938272

#武冈深圳qq群:66250781

#参考资料:Packtpub.Python.Testing.Cookbook.May.2011.pdf


    PyUnit创建于1999年,2001年Python 2.1集成了PyUnit,模块名为unittest(
library/unittest.html),它python自动化测试的重要模块。下面演示在unittest中使用断言。

        class RomanNumeralConverter(object):
            def __init__(self, roman_numeral):
                self.roman_numeral = roman_numeral
                self.digit_map = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I":1}

            def convert_to_decimal(self):
                val = 0
                for char in self.roman_numeral:
                    val += self.digit_map[char]
                return val

        import unittest

        class RomanNumeralConverterTest(unittest.TestCase):
            def test_parsing_millenia(self):
                value = RomanNumeralConverter("M")
                self.assertEquals(1000, value.convert_to_decimal())

            def test_parsing_century(self):
                value = RomanNumeralConverter("C")
                self.assertEquals(100, value.convert_to_decimal())

            def test_parsing_half_century(self):
                value = RomanNumeralConverter("L")
                self.assertEquals(50, value.convert_to_decimal())

            def test_parsing_decade(self):
                value = RomanNumeralConverter("X")
                self.assertEquals(10, value.convert_to_decimal())

            def test_parsing_half_decade(self):
                value = RomanNumeralConverter("V")
                self.assertEquals(5, value.convert_to_decimal())

            def test_parsing_one(self):
                value = RomanNumeralConverter("I")
                self.assertEquals(1, value.convert_to_decimal())

            def test_empty_roman_numeral(self):
                value = RomanNumeralConverter("")
                self.assertTrue(value.convert_to_decimal() == 0)
                self.assertFalse(value.convert_to_decimal() > 0)

            def test_no_roman_numeral(self):
                value = RomanNumeralConverter(None)
                self.assertRaises(TypeError, value.convert_to_decimal)

        if __name__ == "__main__":
            unittest.main()
           
        执行结果:
        ........
        ----------------------------------------------------------------------
        Ran 8 tests in 0.000s

        OK   
   
    相关参考如下:
    assertEquals(first, second[, msg]): Compares first and second expressions; and fails, if they don't have the same value. We can optionally print a special message if there is a failure.
    assertTrue(expression[, msg]): Tests the expression and fails if it is false.We can optionally print a special message if there is a failure.
    assertFalse(expression[, msg]): Tests the expression and fails if it is true.We can optionally print a special message if there is a failure.
    assertRaises(exception, callable, …): Runs the callable, with any arguments, for the callable listed afterwards, and fails if it doesn't raise the exception.
   
    注意要优先使用assertEquals,不得已才使用assertTrue或者assertFalse。因为assertEquals的输出更详细,会输出相应的两个值。使用assertEquals要注意列表等可变对象以及自定义对象等。尽量用assertRaises代替fail,因为代码正确时候,根本不会执行fail语句。fail举例如下:
        import unittest
        class BadTest(unittest.TestCase):
        def test_no_roman_numeral(self):
        value = RomanNumeralConverter(None)
        try:
        value.convert_to_decimal()
        self.fail("Expected a TypeError")
        except TypeError, e:
        pass
       
    另外第3方模块unittest2 ()提供了更多的功能。

阅读(6159) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~