''''' Created on 2014-4-15 @author: Administrator ''' import unittest,my_math class Test(unittest.TestCase): def testIntegers(self): for x in xrange(-10,10): for y in xrange(-10,10): p = my_math.product(x,y) self.failUnless(p == x*y,'Integer multiplication faild') def testFloat(self): for x in xrange(-10,10): for y in xrange(-10,10): x = x/10.0 y = y/10.0 p = my_math.product(x,y) self.failUnless(p == x*y,'Float multiplication faild') if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main() my_math.py ''''' Created on 2014-4-15 @author: Administrator ''' def square(x): ''''' Squares a number and return the result. >>>square(2) 4 >>>square(3) 9 ''' return x*x def product(x,y): if x == 7 and y ==9: return 'bug' else: return x * y #return x*y ''''' if __name__ == '__main__': import doctest, my_math doctest.testmod(my_math) ''' |
unittest.main函数负责运行测试。它会实例化所有TestCase的子类,运行所有名字以test开头的方法。
如果定义了叫做setUp和tearDown的方法,他们就会运行在每个测试方法之前和之后执行。