1. 关于PyUnit
今天联系的内容是单元测试,
python单元测试框架: PyUnit
就像看JUnit ,先看他的Getting Started , A cooks tour 一样,
看PyUnit, 我也是先找这些东西,
PyUnit的首页上有他中文文档的链接:
Chinese translation of the PyUnit documentation
不过我觉得这份文档有点长...
不如把PyUnit下载下来, 直接看自带的sample来的直接.
2. 我关心的问题:创建suite
写testcase,就继承unittest.TestCase, 在我看来这一切都和JUnit(4.0以前)一样,
然而创建suite的方法稍微有些不同, 因此如何创建suite便成了我的关心点
(本来xUnit就是照着JUnit来的, 对我而言, 对比阐述清楚其自身与JUnit之间的相异之处的文章会更好)
PyUnit常见suite有三种方法, 不罗嗦, 直接上代码
(PS,附件是代码的一个压缩包, 里面还包括下面代码中使用到的t01,t02模块)
#! /usr/bin/python
__author__="wjason"
__date__ ="$2009-7-1 15:53:49$"
import unittest
import t01,t02
def suite():
#create suite: method 1
moduls_to_test = ('t01','t02')
alltest = unittest.TestSuite()
for module in map(__import__,moduls_to_test):
#print module
alltest.addTest(unittest.findTestCases(module))
# another two method to add testcase into suite
#create suite: method 2
alltest.addTest(t01.test01('test01aaa'))
#create suite: method 3
alltest.addTest(unittest.makeSuite(t02.test02))
return alltest
if __name__ == "__main__":
#t01.test01("aaa").test01aaa()
#unittest.main()
unittest.main(defaultTest='suite')
3. 总结
除了PyUnit, 我还学到了下面这两个函数的用法:
a. __import__(str) : 得到一个module类型变量
b. map(func, iterable, ...): 对集合应用特定函数.