欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python之PyUnit单元测试实例

程序员文章站 2023-12-12 21:33:10
本文实例讲述了python之pyunit单元测试,与erlang eunit单元测试很像,分享给大家供大家参考。具体方法如下: 1.widget.py文件如下: 复制代...

本文实例讲述了python之pyunit单元测试,与erlang eunit单元测试很像,分享给大家供大家参考。具体方法如下:

1.widget.py文件如下:

复制代码 代码如下:
#!/usr/bin/python
# filename:widget.py

class widget:
def __init__(self, size = (40, 40)):
self.size = size
 
def getsize(self):
return self.size
 
def resize(self, width, height):
if width < 0 or height < 0:
raise valueerror, "illegal size"
self.size = (width, height)
 
def dispose(self):
passdefaulttestcase

2. auto.py文件如下:

复制代码 代码如下:
#!/usr/bin/python
# filename:auto.py
 
import unittest
from widget import widget
 
class widgettestcase(unittest.testcase):
def setup(self):
self.widget = widget()
 
def teardown(self):
self.widget = none
 
def testsize(self):
self.assertequal(self.widget.getsize(), (50, 40))
 
def suite():
suite = unittest.testsuite()
suite.addtest(widgettestcase("testsize"))
return suite
 
if __name__ == "__main__":
unittest.main(defaulttest = 'suite')

3.执行结果如下:

[code]jobin@jobin-desktop:~/work/python/py_unit$ python auto.py
.
----------------------------------------------------------------------
ran 1 test in 0.000s
 
ok
python auto.py
f
======================================================================
fail: testsize (__main__.widgettestcase)
----------------------------------------------------------------------
traceback (most recent call last):
file "auto.py", line 15, in testsize
self.assertequal(self.widget.getsize(), (50, 40))
assertionerror: (40, 40) != (50, 40)
 
----------------------------------------------------------------------
ran 1 test in 0.000s
 
failed (failures=1)
]

希望本文所述对大家的python程序设计有所帮助。

上一篇:

下一篇: