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程序设计有所帮助。

相关文章

Python3.5模块的定义、导入、优化操作图文详解

Python3.5模块的定义、导入、优化操作图文详解

本文实例讲述了Python3.5模块的定义、导入、优化操作。分享给大家供大家参考,具体如下: 1、模块体系大纲 2、模块的定义 模块的本质:是一个.py格式的Python文件。文件...

使用python采集脚本之家电子书资源并自动下载到本地的实例脚本

使用python采集脚本之家电子书资源并自动下载到本地的实例脚本

jb51上面的资源还比较全,就准备用python来实现自动采集信息,与下载啦。 Python具有丰富和强大的库,使用urllib,re等就可以轻松开发出一个网络信息采集器! 下面,是我写...

Python的Bottle框架的一些使用技巧介绍

之前对bottle做过不少的介绍,也写过一些文章来说明bottle的缺点,最近发现其实之前有些地方说的不太公平,所以趁此机会也来更正一下。     bott...

恢复百度云盘本地误删的文件脚本(简单方法)

今天被同步盘搞得焦头烂额。 辛苦码的代码(除了重要的、备份过的)都被删掉了…… 当时我就石化了。。。 随后发现同步盘目录有个delete目录,里面还有manifest.xml,和一堆改了...

Pycharm创建项目时如何自动添加头部信息

Pycharm创建项目时如何自动添加头部信息

1.打开PyCharm,选择File--Settings 2.依次选择Editor---Code Style-- File and Code Templates---Python Sc...