Python之PyUnit单元测试实例

yipeiwu_com6年前Python基础

本文实例讲述了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
jobin@jobin-desktop:~/work/python/py_unit$ 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)
jobin@jobin-desktop:~/work/python/py_unit$[/code]

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

相关文章

python能调用ffmpeg和aria2c吗

是的,Python 可以调用 FFmpeg 和 aria2c 这两个工具。通常,我们通过以下几种方式来实现这一目的: ˂a class="reference-link" name="1. 使用 su...

python 求定积分和不定积分示例

python 求定积分和不定积分示例

求f(x) = sin(x)/x 的不定积分和负无穷到正无穷的定积分 sin(x)/x 的不定积分是信号函数sig ,负无穷到正无穷的定积分为pi import math impor...

Python探索之修改Python搜索路径

当Python执行import语句时,它会在一些路径中搜索Python模块和扩展模块。可以通过sys.path查看这些路径,比如: >>> import sys...

python机器学习包mlxtend的安装和配置详解

python机器学习包mlxtend的安装和配置详解

今天看到了mlxtend的包,看了下example集成得非常简洁。还有一个吸引我的地方是自带了一些data直接可以用,省去了自己造数据或者找数据的处理过程,所以决定安装体验一下。 依赖环...

python实现名片管理系统

python实现名片管理系统

本文实例为大家分享了python实现名片管理系统的具体代码,供大家参考,具体内容如下 系统需求 程序启动,显示名片管理系统欢迎界面,并显示功能菜单 ******************...