qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Python单元测试框架使用unittestpyUnit

 使用Pyunit框架的简单测试
'''''
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的方法,他们就会运行在每个测试方法之前和之后执行。

posted on 2014-04-21 12:50 顺其自然EVO 阅读(312) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜