Python之PyUnit單元測(cè)試實(shí)例
本文實(shí)例講述了Python之PyUnit單元測(cè)試,與erlang eunit單元測(cè)試很像,分享給大家供大家參考。具體方法如下:
1.widget.py文件如下:
# 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文件如下:
# 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.執(zhí)行結(jié)果如下:
[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]
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python庫(kù)Tsmoothie模塊數(shù)據(jù)平滑化異常點(diǎn)抓取
這篇文章主要為大家介紹了python庫(kù)Tsmoothie模塊數(shù)據(jù)平滑化技術(shù)實(shí)現(xiàn)異常點(diǎn)抓取,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06python Airtest自動(dòng)化測(cè)試工具的的使用
本文主要介紹了python Airtest自動(dòng)化測(cè)試工具的的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02詳解Python如何實(shí)現(xiàn)Excel數(shù)據(jù)讀取和寫入
這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)對(duì)EXCEL數(shù)據(jù)進(jìn)行讀取和寫入,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04基于python的Tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
這篇文章主要介紹了基于python的Tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器的相關(guān)資料,還為大家分享了僅用用50行Python代碼實(shí)現(xiàn)的簡(jiǎn)易計(jì)算器,感興趣的小伙伴們可以參考一下2015-12-12