'''''
created on 2014-4-15
@author: administrator
'''
import unittest,my_math
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):
x = x/10.0
y = y/10.0
self.failunless(p == x*y,'float multiplication faild')
if __name__ == "__main__":
#import sys;sys.argv = ['', 'test.testname']
unittest.main()
my_math.py
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的方法,他們就會運作在每個測試方法之前和之後執行。
最新内容請見作者的github頁:http://qaseven.github.io/