详细代码见附件
![](https://i-blog.csdnimg.cn/blog_migrate/be65b7145fe91aca38f3cb2a5b4806fb.gif)
- """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()