我用过的testunit有Nunit,Pyunit,Dunit,从易用性来说Pyunit>Nunit>Dunit,这个是由语言特性决定的.PyUnit有不同的实现,python标准库中有一个自带的. 不过说回来,脚本语言unittest的重要性并没有这么大,因为在每个程序文件中随手定义一些测试函数,直接运行就是.这里unittest 模块存在的意义在于能够打印一些profile信息它的使用非常简单,你的测试类必须是unittest.TestCase的子类,然后定义相应的测试函数,只要把函数名称写成testXXX的格式,然后运行unittest.main(),这些测试函数就会被调用.另外,unittest.TestCase提供了一个函数setUp,这个函数会在每个测试函数被调用之前都调用一次,如果需要做一些初始化处理,可以重载这个函数.unittest模块还提供了一些高级功能,可以查看帮助.
import unittest
class Person:
def age(self):
return 34
def name(self):
return 'bob'
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.man = Person()
print 'set up now'
def test1(self):
self.assertEqual(self.man.age(), 34)
def test2(self):
self.assertEqual(self.man.name(), 'bob')
def test3(self):
self.assertEqual(4+78,23)
if __name__ == '__main__':
unittest.main()
一些相关的链接:
A chapter on Unit Testing in Mark Pilgrim’s Dive Into Python
unittest module documentation, in the Python Library Reference.
UnitTests on the PortlandPatternRepository Wiki, where all the cool ExtremeProgramming kids hang out.
Unit Tests in Extreme Programming: A Gentle Introduction.
Ron Jeffries espouses on the importance of Unit Tests at 100%.
Ron Jeffries writes about the Unit Test in the Extreme Programming practices of C3.
PyUnit’s homepage. |