python 測(cè)試實(shí)現(xiàn)方法
更新時(shí)間:2008年12月24日 23:08:55 作者:
使用python進(jìn)行測(cè)試也足夠簡(jiǎn)明了
1)doctest
使用doctest是一種類似于命令行嘗試的方式,用法很簡(jiǎn)單,如下
def f(n):
"""
>>> f(1)
1
>>> f(2)
2
"""
print(n)
if __name__ == '__main__':
import doctest
doctest.testmod()
應(yīng)該來說是足夠簡(jiǎn)單了,另外還有一種方式doctest.testfile(filename),就是把命令行的方式放在文件里進(jìn)行測(cè)試。
2)unittest
unittest歷史悠久,最早可以追溯到上世紀(jì)七八十年代了,C++,Java里也都有類似的實(shí)現(xiàn),Python里的實(shí)現(xiàn)很簡(jiǎn)單。
unittest在python里主要的實(shí)現(xiàn)方式是TestCase,TestSuite。用法還是例子起步。
from widget import Widget
import unittest
# 執(zhí)行測(cè)試的類
class WidgetTestCase(unittest.TestCase):
def setUp(self):
self.widget = Widget()
def tearDown(self):
self.widget.dispose()
self.widget = None
def testSize(self):
self.assertEqual(self.widget.getSize(), (40, 40))
def testResize(self):
self.widget.resize(100, 100)
self.assertEqual(self.widget.getSize(), (100, 100))
# 測(cè)試
if __name__ == "__main__":
# 構(gòu)造測(cè)試集
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase("testSize"))
suite.addTest(WidgetTestCase("testResize"))
# 執(zhí)行測(cè)試
runner = unittest.TextTestRunner()
runner.run(suite)
簡(jiǎn)單的說,1>構(gòu)造TestCase(測(cè)試用例),其中的setup和teardown負(fù)責(zé)預(yù)處理和善后工作。2>構(gòu)造測(cè)試集,添加用例3>執(zhí)行測(cè)試需要說明的是測(cè)試方法,在Python中有N多測(cè)試函數(shù),主要的有:
TestCase.assert_(expr[, msg])
TestCase.failUnless(expr[, msg])
TestCase.assertTrue(expr[, msg])
TestCase.assertEqual(first, second[, msg])
TestCase.failUnlessEqual(first, second[, msg])
TestCase.assertNotEqual(first, second[, msg])
TestCase.failIfEqual(first, second[, msg])
TestCase.assertAlmostEqual(first, second[, places[, msg]])
TestCase.failUnlessAlmostEqual(first, second[, places[, msg]])
TestCase.assertNotAlmostEqual(first, second[, places[, msg]])
TestCase.failIfAlmostEqual(first, second[, places[, msg]])
TestCase.assertRaises(exception, callable, ...)
TestCase.failUnlessRaises(exception, callable, ...)
TestCase.failIf(expr[, msg])
TestCase.assertFalse(expr[, msg])
TestCase.fail([msg])
使用doctest是一種類似于命令行嘗試的方式,用法很簡(jiǎn)單,如下
復(fù)制代碼 代碼如下:
def f(n):
"""
>>> f(1)
1
>>> f(2)
2
"""
print(n)
if __name__ == '__main__':
import doctest
doctest.testmod()
應(yīng)該來說是足夠簡(jiǎn)單了,另外還有一種方式doctest.testfile(filename),就是把命令行的方式放在文件里進(jìn)行測(cè)試。
2)unittest
unittest歷史悠久,最早可以追溯到上世紀(jì)七八十年代了,C++,Java里也都有類似的實(shí)現(xiàn),Python里的實(shí)現(xiàn)很簡(jiǎn)單。
unittest在python里主要的實(shí)現(xiàn)方式是TestCase,TestSuite。用法還是例子起步。
復(fù)制代碼 代碼如下:
from widget import Widget
import unittest
# 執(zhí)行測(cè)試的類
class WidgetTestCase(unittest.TestCase):
def setUp(self):
self.widget = Widget()
def tearDown(self):
self.widget.dispose()
self.widget = None
def testSize(self):
self.assertEqual(self.widget.getSize(), (40, 40))
def testResize(self):
self.widget.resize(100, 100)
self.assertEqual(self.widget.getSize(), (100, 100))
# 測(cè)試
if __name__ == "__main__":
# 構(gòu)造測(cè)試集
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase("testSize"))
suite.addTest(WidgetTestCase("testResize"))
# 執(zhí)行測(cè)試
runner = unittest.TextTestRunner()
runner.run(suite)
簡(jiǎn)單的說,1>構(gòu)造TestCase(測(cè)試用例),其中的setup和teardown負(fù)責(zé)預(yù)處理和善后工作。2>構(gòu)造測(cè)試集,添加用例3>執(zhí)行測(cè)試需要說明的是測(cè)試方法,在Python中有N多測(cè)試函數(shù),主要的有:
TestCase.assert_(expr[, msg])
TestCase.failUnless(expr[, msg])
TestCase.assertTrue(expr[, msg])
TestCase.assertEqual(first, second[, msg])
TestCase.failUnlessEqual(first, second[, msg])
TestCase.assertNotEqual(first, second[, msg])
TestCase.failIfEqual(first, second[, msg])
TestCase.assertAlmostEqual(first, second[, places[, msg]])
TestCase.failUnlessAlmostEqual(first, second[, places[, msg]])
TestCase.assertNotAlmostEqual(first, second[, places[, msg]])
TestCase.failIfAlmostEqual(first, second[, places[, msg]])
TestCase.assertRaises(exception, callable, ...)
TestCase.failUnlessRaises(exception, callable, ...)
TestCase.failIf(expr[, msg])
TestCase.assertFalse(expr[, msg])
TestCase.fail([msg])
您可能感興趣的文章:
相關(guān)文章
在django中使用post方法時(shí),需要增加csrftoken的例子
這篇文章主要介紹了在django中使用post方法時(shí),需要增加csrftoken的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python調(diào)用接口合并Excel表代碼實(shí)例
這篇文章主要介紹了Python調(diào)用接口合并Excel表代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03python在回調(diào)函數(shù)中獲取返回值的方法
今天小編就為大家分享一篇python在回調(diào)函數(shù)中獲取返回值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02利用Python實(shí)現(xiàn)繪制3D愛心的代碼分享
最近你是否也被李峋的愛心跳動(dòng)代碼所感動(dòng),心動(dòng)不如行動(dòng),相同的代碼很多,我們今天換一個(gè)玩法!構(gòu)建一個(gè)三維的跳動(dòng)愛心!嗯!這篇博客本著開源的思想!不是說誰(shuí)對(duì)浪漫過敏的2022-11-11Python桌面應(yīng)用開發(fā)實(shí)戰(zhàn)之PyQt的安裝使用
這篇文章主要給大家介紹了關(guān)于Python桌面應(yīng)用開發(fā)實(shí)戰(zhàn)之PyQt的安裝使用,PyQt是一個(gè)功能強(qiáng)大的Python庫(kù),用于創(chuàng)建圖形用戶界面(GUI)應(yīng)用程序,需要的朋友可以參考下2023-08-08Tensorflow實(shí)現(xiàn)酸奶銷量預(yù)測(cè)分析
這篇文章主要為大家詳細(xì)介紹了Tensorflow酸奶銷量預(yù)測(cè)分析,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07