手机
当前位置:查字典教程网 >脚本专栏 >python >Python之PyUnit单元测试实例
Python之PyUnit单元测试实例
摘要:本文实例讲述了Python之PyUnit单元测试,与erlangeunit单元测试很像,分享给大家供大家参考。具体方法如下:1.widget...

本文实例讲述了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之PyUnit单元测试实例】相关文章:

python单链表实现代码实例

python算法学习之基数排序实例

python创建和使用字典实例详解

python使用urllib2模块获取gravatar头像实例

Python中字典(dict)和列表(list)的排序方法实例

Python之eval()函数危险性浅析

python在windows下实现备份程序实例

python二分法实现实例

python 七种邮件内容发送方法实例

python实现进程间通信简单实例

精品推荐
分类导航