Python之PyUnit单元测试实例

yipeiwu_com5年前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多进程控制学习小结

python多进程控制学习小结

前言: python多进程,经常在使用,却没有怎么系统的学习过,官网上面讲得比较细,结合自己的学习,整理记录下官网:https://docs.python.org/3/library/m...

Python基于滑动平均思想实现缺失数据填充的方法

在时序数据处理过程中,我们经常会遇到由于现实中的种种原因导致获取的数据缺失的情况,这里的数据缺失不单单是指为‘NaN'的数据,比如在AQI数据中,0是不可能出现的,这时候如果数据中出现了...

python opencv实现任意角度的透视变换实例代码

本文主要分享的是一则python+opencv实现任意角度的透视变换的实例,具体如下: # -*- coding:utf-8 -*- import cv2 import numpy...

使用Pyhton集合set()实现成果查漏的例子

问题:不同版本提交的城市文件夹数量固定,怎样确定本版本成果中缺少了哪些城市? 背景:已有参照文件作为标准,利用取差集的方法 #-*- coding: utf-8 -*- #以上版本成...

python回调函数的使用方法

有两种类型的回调函数:复制代码 代码如下:blocking callbacks (also known as synchronous callbacks or just callback...