Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29955603
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2010-05-31 22:10:29

Python unittest 模块

示例:

import unittest

import json

 

def _removeWhitespace(str):

    return str.replace(" ", "")

 

class JsonTest(unittest.TestCase):

    def testReadEmptyObject(self):

        obj = json.read("{}")

        self.assertEqual({}, obj)

 

    def testWriteEmptyObject(self):

        s = json.write({})

        self.assertEqual("{}", _removeWhitespace(s))

 

    def testReadStringValue(self):

        obj = json.read('{ "name" : "Patrick" }')

        self.assertEqual({ "name" : "Patrick" }, obj)

 

 

    def doReadBadObjectKey(self):

        json.read('{ 44 : "age" }')

 

 

    def testWriteStringValue(self):

        s = json.write({ "name" : "Patrick" })

        self.assertEqual('{"name":"Patrick"}', _removeWhitespace(s))

 

    def testReadIntegerValue(self):

        obj = json.read('{ "age" : 44 }')

        self.assertEqual({ "age" : 44 }, obj)

 

    def testReadNegativeIntegerValue(self):

        obj = json.read('{ "key" : -44 }')

        self.assertEqual({ "key" : -44 }, obj)

       

    def testReadFloatValue(self):

        obj = json.read('{ "age" : 44.5 }')

        self.assertEqual({ "age" : 44.5 }, obj)

 

    def testReadNegativeFloatValue(self):

        obj = json.read(' { "key" : -44.5 } ')

        self.assertEqual({ "key" : -44.5 }, obj)

 

 

    def doReadBadNumber(self):

        json.read('-44.4.4')

 

    def testReadSmallObject(self):

        obj = json.read('{ "name" : "Patrick", "age":44} ')

        self.assertEqual({ "age" : 44, "name" : "Patrick" }, obj)       

 

    def testReadEmptyArray(self):

        obj = json.read('[]')

        self.assertEqual([], obj)

 

    def testWriteEmptyArray(self):

        self.assertEqual("[]", _removeWhitespace(json.write([])))

 

    def testReadSmallArray(self):

        obj = json.read(' [ "a" , "b", "c" ] ')

        self.assertEqual(["a", "b", "c"], obj)

 

    def testWriteSmallArray(self):

        self.assertEqual('[1,2,3,4]', _removeWhitespace(json.write([1, 2, 3, 4])))

 

    def testWriteSmallObject(self):

        s = json.write({ "name" : "Patrick", "age": 44 })

        self.assertEqual('{"age":44,"name":"Patrick"}', _removeWhitespace(s))

 

    def testWriteFloat(self):

        self.assertEqual("3.445567", _removeWhitespace(json.write(3.44556677)))

 

    def testReadTrue(self):

        self.assertEqual(True, json.read("true"))

 

    def testReadFalse(self):

        self.assertEqual(False, json.read("false"))

 

    def testReadNull(self):

        self.assertEqual(None, json.read("null"))

 

    def testWriteTrue(self):

        self.assertEqual("true", _removeWhitespace(json.write(True)))

 

    def testWriteFalse(self):

        self.assertEqual("false", _removeWhitespace(json.write(False)))

 

    def testWriteNull(self):

        self.assertEqual("null", _removeWhitespace(json.write(None)))

 

    def testReadArrayOfSymbols(self):

        self.assertEqual([True, False, None], json.read(" [ true, false,null] "))

 

    def testWriteArrayOfSymbolsFromList(self):

        self.assertEqual("[true,false,null]", _removeWhitespace(json.write([True, False, None])))

 

    def testWriteArrayOfSymbolsFromTuple(self):

        self.assertEqual("[true,false,null]", _removeWhitespace(json.write((True, False, None))))

 

    def testReadComplexObject(self):

        src = '''

    { "name": "Patrick", "age" : 44, "Employed?" : true, "Female?" : false, "grandchildren":null }

'''

        obj = json.read(src)

        self.assertEqual({"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None}, obj)

 

    def testReadLongArray(self):

        src = '''[    "used",

    "abused",

    "confused",

    true, false, null,

    1,

    2,

    [3, 4, 5]]

'''

        obj = json.read(src)

        self.assertEqual(["used","abused","confused", True, False, None,

                          1,2,[3,4,5]], obj)

 

    def testReadIncompleteArray(self):

        self.assertRaises(json.ReadException, self.doReadIncompleteArray)

 

    def doReadIncompleteArray(self):

        json.read('[')

 

    def testReadComplexArray(self):

        src = '''

[

    { "name": "Patrick", "age" : 44,

      "Employed?" : true, "Female?" : false,

      "grandchildren":null },

    "used",

    "abused",

    "confused",

    1,

    2,

    [3, 4, 5]

]

'''

        obj = json.read(src)

        self.assertEqual([{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None},

                          "used","abused","confused",

                          1,2,[3,4,5]], obj)

 

    def testWriteComplexArray(self):

        obj = [{"name":"Patrick","age":44,"Employed?":True,"Female?":False,"grandchildren":None},

               "used","abused","confused",

               1,2,[3,4,5]]

        self.assertEqual('[{"Female?":false,"age":44,"name":"Patrick","grandchildren":null,"Employed?":true},"used","abused","confused",1,2,[3,4,5]]',

                         _removeWhitespace(json.write(obj)))

 

 

    def testReadWriteCopies(self):

        orig_obj = {'a':' " '}

        json_str = json.write(orig_obj)

        copy_obj = json.read(json_str)

        self.assertEqual(orig_obj, copy_obj)

        self.assertEqual(True, orig_obj == copy_obj)

        self.assertEqual(False, orig_obj is copy_obj)

 

    def testStringEncoding(self):

        s = json.write([1, 2, 3])

        self.assertEqual(unicode("[1,2,3]", "utf-8"), _removeWhitespace(s))

 

    def testReadEmptyObjectAtEndOfArray(self):

        self.assertEqual(["a","b","c",{}],

                         json.read('["a","b","c",{}]'))

 

    def testReadEmptyObjectMidArray(self):

        self.assertEqual(["a","b",{},"c"],

                         json.read('["a","b",{},"c"]'))

 

    def testReadClosingObjectBracket(self):

        self.assertEqual({"a":[1,2,3]}, json.read('{"a":[1,2,3]}'))

 

    def testAnotherDoubleSlashComment(self):

        obj = json.read('[1 , // xzy\n2]')

        self.assertEqual(obj, [1, 2])

 

    def testAnotherSlashStarComment(self):

        obj = json.read('[1,/* xzy */2]')

        self.assertEqual(obj, [1, 2])

 

    def testEmptyObjectInList(self):

        obj = json.read('[{}]')

        self.assertEqual([{}], obj)

 

    def testObjectInListWithSlashStarComment(self):

        obj1 = json.read('[{} /*Comment*/]')

        self.assertEqual([{}], obj1)

 

    def testObjectWithEmptyList(self):

        obj = json.read('{"test": [] }')

        self.assertEqual({"test":[]}, obj)

 

    def testObjectWithNonEmptyList(self):

        obj = json.read('{"test": [3, 4, 5] }')

        self.assertEqual({"test":[3, 4, 5]}, obj)

 

    def testCommentInObjectWithListValue(self):

        obj2 = json.read('{"test": [] /*Comment*/}')

        self.assertEqual({"test":[]}, obj2)

 

    def testWriteLong(self):

        self.assertEqual("12345678901234567890", json.write(12345678901234567890))

       

def main():

    unittest.main()

 

if __name__ == '__main__':

    main()

 

 

结论:有了单元测试框架我们可以针对我们每一个类单独编写各自的一个单元测试出来。依据输入INPUTOUTPUT来做测试。判断是否这个函数处理正常。

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