自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python中的兩個(gè)測(cè)試工具

開發(fā) 后端 測(cè)試
本文介紹了兩個(gè)Python中的測(cè)試工具:doctest和unittest,并配以簡(jiǎn)單的例子來說明這兩個(gè)測(cè)試模塊的使用方法,希望能對(duì)讀者有所幫助。

 [[275103]]

當(dāng)我們?cè)趯懗绦虻臅r(shí)候,我們需要通過測(cè)試來驗(yàn)證程序是否出錯(cuò)或者存在問題,但是,編寫大量的測(cè)試來確保程序的每個(gè)細(xì)節(jié)都沒問題會(huì)顯得很繁瑣。在Python中,我們可以借助一些標(biāo)準(zhǔn)模塊來幫助我們自動(dòng)完成測(cè)試過程,比如:

  •  unittest: 一個(gè)通用的測(cè)試框架;
  •  doctest: 一個(gè)更簡(jiǎn)單的模塊,是為檢查文檔而設(shè)計(jì)的,但也非常適合用來編寫單元測(cè)試。

下面,筆者將會(huì)簡(jiǎn)單介紹這兩個(gè)模塊在測(cè)試中的應(yīng)用。

doctest

doctest模塊會(huì)搜索那些看起來像是python交互式會(huì)話中的代碼片段,然后嘗試執(zhí)行并驗(yàn)證結(jié)果。下面我們以doctest.testmod為例,函數(shù)doctest.testmod會(huì)讀取模塊中的所有文檔字符串,查找看起來像是從交互式解釋器中摘取的示例,再檢查這些示例是否反映了實(shí)際情況。

我們先創(chuàng)建示例代碼文件test_string_lower.py,完整代碼如下: 

  1. # -*- coding: utf-8 -*-  
  2. def string_lower(string):  
  3.     '''  
  4.     返回一個(gè)字符串的小寫  
  5.     :param string: type: str  
  6.     :return: the lower of input string  
  7.     >>> string_lower('AbC')  
  8.     'abc'  
  9.     >>> string_lower('ABC')  
  10.     'abc'  
  11.     >>> string_lower('abc')  
  12.     'abc'  
  13.     '''  
  14.     return string.lower()  
  15. if __name__ == '__main__':  
  16.     import doctest, test_string_lower  
  17.     doctest.testmod(test_string_lower) 

首先先對(duì)程序進(jìn)行說明,函數(shù)string_lower用于返回輸入字符串的小寫,函數(shù)中的注釋中,一共包含了3個(gè)測(cè)試實(shí)例,期望盡可能地包含各種測(cè)試情況,接著在主函數(shù)中導(dǎo)入doctest, test_string_lower,再運(yùn)行doctest中的testmod函數(shù)即可進(jìn)行測(cè)試。

接著,我們開始測(cè)試。首先,在命令行中輸入python test_string_lower.py,運(yùn)行后會(huì)發(fā)現(xiàn)什么都沒有輸出,但這其實(shí)是件好事,它表明程序中的所有測(cè)試都通過了!那么,如果我們想要獲得更多的輸出呢?可在運(yùn)行腳本的時(shí)候增加參數(shù)-v,這時(shí)候命令變成python test_string_lower.py -v,輸出的結(jié)果如下: 

  1. Trying:  
  2.     string_lower('AbC')  
  3. Expecting:  
  4.     'abc'  
  5. ok  
  6. Trying:  
  7.     string_lower('ABC')  
  8. Expecting:  
  9.     'abc'  
  10. ok  
  11. Trying:  
  12.     string_lower('abc')  
  13. Expecting:  
  14.     'abc'  
  15. ok  
  16. 1 items had no tests:  
  17.     test_string_lower  
  18. 1 items passed all tests:  
  19.    3 tests in test_string_lower.string_lower  
  20. 3 tests in 2 items.  
  21. 3 passed and 0 failed.  
  22. Test passed. 

可以看到,程序測(cè)試的背后還是發(fā)生了很多事。接著,我們嘗試著程序出錯(cuò)的情況,比如我們不小心把函數(shù)的返回寫成了: 

  1. return string.upper() 

這其實(shí)是返回輸入字符串的大寫了,而我們測(cè)試的實(shí)例卻返回了輸入字符串的小寫,再運(yùn)行該腳本(加上參數(shù)-v),輸出的結(jié)果如下: 

  1. Failed example:  
  2.     string_lower('abc')  
  3. Expected:  
  4.     'abc'  
  5. Got:  
  6.     'ABC'  
  7. 1 items had no tests:  
  8.     test_string_lower  
  9. **********************************************************************  
  10. 1 items had failures:  
  11.    3 of   3 in test_string_lower.string_lower  
  12. 3 tests in 2 items.  
  13. 0 passed and 3 failed.  
  14. ***Test Failed*** 3 failures. 

這時(shí)候,程序測(cè)試失敗,它不僅捕捉到了bug,還清楚地指出錯(cuò)誤出在什么地方。我們不難把這個(gè)程序修改過來。

關(guān)于doctest模塊的更詳細(xì)的使用說明,可以參考網(wǎng)址:https://docs.python.org/2/library/doctest.html 。

unittest

unittest類似于流行的Java測(cè)試框架JUnit,它比doctest更靈活,更強(qiáng)大,能夠幫助你以結(jié)構(gòu)化的方式來編寫龐大而詳盡的測(cè)試集。

我們以一個(gè)簡(jiǎn)單的示例入手,首先我們編寫my_math.py腳本,代碼如下: 

  1. # -*- coding: utf-8 -*-  
  2. def product(x, y):  
  3.     '''  
  4.     :param x: int, float  
  5.     :param y: int, float  
  6.     :return:  x * y  
  7.     '''  
  8.     return x * y 

該函數(shù)實(shí)現(xiàn)的功能為:輸入兩個(gè)數(shù)x, y, 返回這兩個(gè)數(shù)的乘積。接著是test_my_math.py腳本,完整的代碼如下: 

  1. import unittest, my_math  
  2. class ProductTestcase(unittest.TestCase):  
  3.     def setUp(self):  
  4.         print('begin test')  
  5.     def test_integers(self):  
  6.         for x in range(-10, 10):  
  7.             for y in range(-10, 10):  
  8.                 p = my_math.product(x, y)  
  9.                 self.assertEqual(p, x*y, 'integer multiplication failed')  
  10.     def test_floats(self):  
  11.         for x in range(-10, 10):  
  12.             for y in range(-10, 10):  
  13.                 xx = x/10  
  14.                 yy = y/10  
  15.                 p = my_math.product(x, y)  
  16.                 self.assertEqual(p, x * y, 'integer multiplication failed')  
  17. if __name__ == '__main__':  
  18.     unittest.main() 

函數(shù)unittest.main負(fù)責(zé)替你運(yùn)行測(cè)試:在測(cè)試方法前執(zhí)行setUp方法,示例化所有的TestCase子類,并運(yùn)行所有名稱以test打頭的方法。assertEqual方法檢車指定的條件(這里是相等),以判斷指定的測(cè)試是成功了還是失敗了。

接著,我們運(yùn)行前面的測(cè)試,輸出的結(jié)果如下: 

  1. begin test  
  2. .begin test  
  3.  
  4. ----------------------------------------------------------------------  
  5. Ran 2 tests in 0.001s  
  6. OK 

可以看到,該程序運(yùn)行了兩個(gè)測(cè)試,每個(gè)測(cè)試前都會(huì)輸出'begin test',.表示測(cè)試成功,若測(cè)試失敗,則返回的是F。

接著模擬測(cè)試出錯(cuò)的情形,將my_math函數(shù)中的product方法改成返回: 

  1. return x + y 

再運(yùn)行測(cè)試腳本,輸出的結(jié)果如下: 

  1. begin test  
  2. Fbegin test  
  3. F 
  4.  ======================================================================  
  5. FAIL: test_floats (__main__.ProductTestcase)  
  6. ----------------------------------------------------------------------  
  7. Traceback (most recent call last):  
  8.   File "test_my_math.py", line 20, in test_floats  
  9.     self.assertEqual(p, x * y, 'integer multiplication failed')  
  10. AssertionError: -2.0 != 1.0 : integer multiplication failed  
  11. ======================================================================  
  12. FAIL: test_integers (__main__.ProductTestcase)  
  13. ----------------------------------------------------------------------  
  14. Traceback (most recent call last):  
  15.   File "test_my_math.py", line 12, in test_integers  
  16.     self.assertEqual(p, x*y, 'integer multiplication failed')  
  17. AssertionError: -20 != 100 : integer multiplication failed  
  18. ----------------------------------------------------------------------  
  19. Ran 2 tests in 0.001s  
  20. FAILED (failures=2

兩條測(cè)試都未通過,返回的是F,并幫助你指出了錯(cuò)誤的地方,接下來,你應(yīng)該能快速地修復(fù)這個(gè)bug。

關(guān)于unittest模塊的更加詳細(xì)的說明,可以參考網(wǎng)址:https://docs.python.org/3/library/unittest.html 。

總結(jié)

本文介紹了兩個(gè)Python中的測(cè)試工具:doctest和unittest,并配以簡(jiǎn)單的例子來說明這兩個(gè)測(cè)試模塊的使用方法,希望能對(duì)讀者有所幫助~ 

 

責(zé)任編輯:龐桂玉 來源: Python中文社區(qū)
相關(guān)推薦

2009-09-17 10:11:50

Linux文件Linux系統(tǒng)性能測(cè)試

2024-05-24 09:57:34

2018-11-19 15:08:21

Python測(cè)試工具pytest插件

2019-08-20 08:00:00

JavaScript測(cè)試工具前端

2013-11-13 10:49:50

2017-09-26 08:51:25

2018-01-16 11:20:08

2009-06-26 10:22:58

JSF測(cè)試

2011-05-31 18:09:05

動(dòng)態(tài)測(cè)試

2020-04-17 10:13:51

Python開發(fā)工具

2009-03-31 09:49:40

Rational功能測(cè)試性能測(cè)試

2013-07-26 09:51:12

網(wǎng)站性能網(wǎng)站測(cè)試性能測(cè)試

2019-03-15 09:17:22

Web測(cè)試工具

2013-08-13 09:43:59

響應(yīng)式免費(fèi)測(cè)試工具響應(yīng)式設(shè)計(jì)

2011-10-09 11:00:17

2012-06-25 14:30:48

Web

2022-11-28 11:31:37

2023-10-10 18:20:22

開源API

2022-01-24 16:55:09

LinuxTCP工具

2019-06-13 05:20:17

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)