apihelpertest.py源代码分析

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>"""Unit test for apihelper.py This program is part of "Dive Into Python", a free Python book for experienced programmers. Visit http://diveintopython.org/ for the latest version. """ __author__ = "Mark Pilgrim ([email protected])" __version__ = "$Revision: 1.4 $" __date__ = "$Date: 2004/05/05 21:57:19 $" __copyright__ = "Copyright (c) 2001 Mark Pilgrim" __license__ = "Python" import unittest #导入unitest模块,用于集成测试 import apihelper #导入apihelper模块 import sys #导入sys模块 from StringIO import StringIO #导入StringIO包中的StringIO模块 class Redirector(unittest.TestCase): #定义一个Redirector类 def setUp(self): #定义setUp函数 self.savestdout = sys.stdout #定义类的字段savestdout,赋为标准输出 self.redirect = StringIO() %定义类的redirect字段为StringIO的返回值 sys.stdout = self.redirect %将标准输出定位到本类的redirect字段 def tearDown(self): #定义tearDown类 sys.stdout = self.savestdout #将标准输出定位到本类的字段savestdout class KnownValues(Redirector): #定义KnownValues类 def testApiHelper(self): #定义testApiHelper函数 """info should return known result for apihelper""" apihelper.info(apihelper) #列出apihelper这个类的所有方法 self.redirect.seek(0) #从StringIO()的开始位置进行读取或写入 self.assertEqual(self.redirect.read(), """info Print methods and doc strings. Takes module, class, list, dictionary, or string. """) #确保两者相同 class ParamChecks(Redirector): #定义ParamChecks类 def testSpacing(self): #定义一个testSpacing方法 """info should honor spacing argument""" apihelper.info(apihelper, spacing=20) #列出apihelper这个类的所有方法,方法与其描述之间相隔20个空格 self.redirect.seek(0) #从StringIO()的开始处读取或写入 self.assertEqual(self.redirect.read(), """info Print methods and doc strings. Takes module, class, list, dictionary, or string. """) #确保二者相同 def testCollapse(self): #定义testCollaspse方法 """info should honor collapse argument""" apihelper.info(apihelper, collapse=0) #collapse置为0,使apihelper的所有方法将在一行内输出 self.redirect.seek(0) #将StringIO()置为开始处读取或写入 self.assertEqual(self.redirect.read(), """info Print methods and doc strings. Takes module, class, list, dictionary, or string. """) class BadInput(unittest.TestCase): def testNoObject(self): """info should fail with no object""" self.assertRaises(TypeError, apihelper.info, spacing=20) #测试经常希望检查在某个环境中是否出现异常。如果期待的异常没有抛出,测试将失败。这很容易做到 #为此,TestCase有一个assertRaises方法。此方法的前两个参数是应该出现在“except”语句中的异常和可调用对象。剩余的参数是应该传递给可调用对象的参数 if __name__ == "__main__": unittest.main()

你可能感兴趣的:(C++,c,python,C#)