详细代码见附件
- """Unit test for odbchelper.py
- This program is part of "Dive Into Python", a free Python book for
- experienced programmers. Visit http://diveintopython.org/ for the
- latest version.
- """
- #加载单元测试模块
- import unittest
- #加载 你的编写的模块
- import odbchelper
- #测试中包括:
- #1.正面测试
- #2.负面测试
- #3.完备性测试:A状态->B状态 ->A状态
- #正面测试
- class GoodInput(unittest.TestCase): #这里要继承 unittest.TestCase
- #编写测试用例,以test 开头
- def testBlank(self):
- """buildConnectionString handles empty dictionary"""
- self.assertEqual("", odbchelper.buildConnectionString({}))
- def testKnownValue(self):
- """buildConnectionString returns known result with known input"""
- params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
- knownItems = params.items()
- knownItems.sort()
- knownString = repr(knownItems)
- result = odbchelper.buildConnectionString(params)
- resultItems = [tuple(e.split("=")) for e in result.split(";")]
- resultItems.sort()
- resultString = repr(resultItems)
- self.assertEqual(knownString, resultString)
- #负面测试
- class BadInput(unittest.TestCase):
- def testString(self):
- """buildConnectionString should fail with string input"""
- self.assertRaises(AttributeError, odbchelper.buildConnectionString, "")
- def testList(self):
- """buildConnectionString should fail with list input"""
- self.assertRaises(AttributeError, odbchelper.buildConnectionString, [])
- def testTuple(self):
- """buildConnectionString should fail with tuple input"""
- self.assertRaises(AttributeError, odbchelper.buildConnectionString, ())
- if __name__ == "__main__":
- unittest.main()

本文详细介绍了如何使用unittest框架进行正向、负向及完备性测试,针对odbchelper.py模块进行了深入测试,确保其功能正常运行。


被折叠的 条评论
为什么被折叠?



