python 測試實現(xiàn)方法
更新時間:2008年12月24日 23:08:55 作者:
使用python進行測試也足夠簡明了
1)doctest
使用doctest是一種類似于命令行嘗試的方式,用法很簡單,如下
def f(n):
"""
>>> f(1)
1
>>> f(2)
2
"""
print(n)
if __name__ == '__main__':
import doctest
doctest.testmod()
應(yīng)該來說是足夠簡單了,另外還有一種方式doctest.testfile(filename),就是把命令行的方式放在文件里進行測試。
2)unittest
unittest歷史悠久,最早可以追溯到上世紀(jì)七八十年代了,C++,Java里也都有類似的實現(xiàn),Python里的實現(xiàn)很簡單。
unittest在python里主要的實現(xiàn)方式是TestCase,TestSuite。用法還是例子起步。
from widget import Widget
import unittest
# 執(zhí)行測試的類
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))
# 測試
if __name__ == "__main__":
# 構(gòu)造測試集
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase("testSize"))
suite.addTest(WidgetTestCase("testResize"))
# 執(zhí)行測試
runner = unittest.TextTestRunner()
runner.run(suite)
簡單的說,1>構(gòu)造TestCase(測試用例),其中的setup和teardown負責(zé)預(yù)處理和善后工作。2>構(gòu)造測試集,添加用例3>執(zhí)行測試需要說明的是測試方法,在Python中有N多測試函數(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是一種類似于命令行嘗試的方式,用法很簡單,如下
復(fù)制代碼 代碼如下:
def f(n):
"""
>>> f(1)
1
>>> f(2)
2
"""
print(n)
if __name__ == '__main__':
import doctest
doctest.testmod()
應(yīng)該來說是足夠簡單了,另外還有一種方式doctest.testfile(filename),就是把命令行的方式放在文件里進行測試。
2)unittest
unittest歷史悠久,最早可以追溯到上世紀(jì)七八十年代了,C++,Java里也都有類似的實現(xiàn),Python里的實現(xiàn)很簡單。
unittest在python里主要的實現(xiàn)方式是TestCase,TestSuite。用法還是例子起步。
復(fù)制代碼 代碼如下:
from widget import Widget
import unittest
# 執(zhí)行測試的類
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))
# 測試
if __name__ == "__main__":
# 構(gòu)造測試集
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase("testSize"))
suite.addTest(WidgetTestCase("testResize"))
# 執(zhí)行測試
runner = unittest.TextTestRunner()
runner.run(suite)
簡單的說,1>構(gòu)造TestCase(測試用例),其中的setup和teardown負責(zé)預(yù)處理和善后工作。2>構(gòu)造測試集,添加用例3>執(zhí)行測試需要說明的是測試方法,在Python中有N多測試函數(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方法時,需要增加csrftoken的例子
這篇文章主要介紹了在django中使用post方法時,需要增加csrftoken的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python在回調(diào)函數(shù)中獲取返回值的方法
今天小編就為大家分享一篇python在回調(diào)函數(shù)中獲取返回值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02Python桌面應(yīng)用開發(fā)實戰(zhàn)之PyQt的安裝使用
這篇文章主要給大家介紹了關(guān)于Python桌面應(yīng)用開發(fā)實戰(zhàn)之PyQt的安裝使用,PyQt是一個功能強大的Python庫,用于創(chuàng)建圖形用戶界面(GUI)應(yīng)用程序,需要的朋友可以參考下2023-08-08Tensorflow實現(xiàn)酸奶銷量預(yù)測分析
這篇文章主要為大家詳細介紹了Tensorflow酸奶銷量預(yù)測分析,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07