论坛首页 编程语言技术论坛

python 面向对象入门 - 之 单元测试

浏览 3008 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-03-02  
详细代码见附件

"""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()
  • py.rar (1.3 KB)
  • 下载次数: 18
   发表时间:2010-03-11  
where is the TestSuite?
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics