??當我們在寫程序的時候,我們需要通過測試來驗證程序是否出錯或者存在問題,但是,編寫大量的測試來確保程序的每個細節都沒問題會顯得很繁瑣。在Python中,我們可以借助一些標準模塊來幫助我們自動完成測試過程,比如:
- unittest: 一個通用的測試框架;
- doctest: 一個更簡單的模塊,是為檢查文檔而設計的,但也非常適合用來編寫單元測試。
??下面,筆者將會簡單介紹這兩個模塊在測試中的應用。
doctest
??doctest模塊會搜索那些看起來像是python交互式會話中的代碼片段,然后嘗試執行并驗證結果。下面我們以doctest.testmod為例,函數doctest.testmod會讀取模塊中的所有文檔字符串,查找看起來像是從交互式解釋器中摘取的示例,再檢查這些示例是否反映了實際情況。
??我們先創建示例代碼文件test_string_lower.py,完整代碼如下:
# -*- coding: utf-8 -*-
def
string_lower
(
string
)
:
'''
返回一個字符串的小寫
:param string: type: str
:return: the lower of input string
>>> string_lower('AbC')
'abc'
>>> string_lower('ABC')
'abc'
>>> string_lower('abc')
'abc'
'''
return
string
.
lower
(
)
if
__name__
==
'__main__'
:
import
doctest
,
test_string_lower
doctest
.
testmod
(
test_string_lower
)
首先先對程序進行說明,函數string_lower用于返回輸入字符串的小寫,函數中的注釋中,一共包含了3個測試實例,期望盡可能地包含各種測試情況,接著在主函數中導入doctest, test_string_lower,再運行doctest中的testmod函數即可進行測試。
??接著,我們開始測試。首先,在命令行中輸入
python test_string_lower.py
,運行后會發現什么都沒有輸出,但這其實是件好事,它表明程序中的所有測試都通過了!那么,如果我們想要獲得更多的輸出呢?可在運行腳本的時候增加參數
-v
,這時候命令變成
python test_string_lower.py -v
,輸出的結果如下:
Trying:
string_lower
(
'AbC'
)
Expecting:
'abc'
ok
Trying:
string_lower
(
'ABC'
)
Expecting:
'abc'
ok
Trying:
string_lower
(
'abc'
)
Expecting:
'abc'
ok
1 items had no tests:
test_string_lower
1 items passed all tests:
3 tests
in
test_string_lower.string_lower
3 tests
in
2 items.
3 passed and 0 failed.
Test passed.
可以看到,程序測試的背后還是發生了很多事。接著,我們嘗試著程序出錯的情況,比如我們不小心把函數的返回寫成了:
return
string
.
upper
(
)
這其實是返回輸入字符串的大寫了,而我們測試的實例卻返回了輸入字符串的小寫,再運行該腳本(加上參數
-v
),輸出的結果如下:
Failed example:
string_lower
(
'abc'
)
Expected:
'abc'
Got:
'ABC'
1 items had no tests:
test_string_lower
**********************************************************************
1 items had failures:
3 of 3
in
test_string_lower.string_lower
3 tests
in
2 items.
0 passed and 3 failed.
***Test Failed*** 3 failures.
這時候,程序測試失敗,它不僅捕捉到了bug,還清楚地指出錯誤出在什么地方。我們不難把這個程序修改過來。
??關于doctest模塊的更詳細的使用說明,可以參考網址:https://docs.python.org/2/library/doctest.html 。
unittest
?? unittest類似于流行的Java測試框架JUnit,它比doctest更靈活,更強大,能夠幫助你以結構化的方式來編寫龐大而詳盡的測試集。
??我們以一個簡單的示例入手,首先我們編寫my_math.py腳本,代碼如下:
# -*- coding: utf-8 -*-
def
product
(
x
,
y
)
:
'''
:param x: int, float
:param y: int, float
:return: x * y
'''
return
x
*
y
該函數實現的功能為:輸入兩個數x, y, 返回這兩個數的乘積。接著是test_my_math.py腳本,完整的代碼如下:
import
unittest
,
my_math
class
ProductTestcase
(
unittest
.
TestCase
)
:
def
setUp
(
self
)
:
print
(
'begin test'
)
def
test_integers
(
self
)
:
for
x
in
range
(
-
10
,
10
)
:
for
y
in
range
(
-
10
,
10
)
:
p
=
my_math
.
product
(
x
,
y
)
self
.
assertEqual
(
p
,
x
*
y
,
'integer multiplication failed'
)
def
test_floats
(
self
)
:
for
x
in
range
(
-
10
,
10
)
:
for
y
in
range
(
-
10
,
10
)
:
x
=
x
/
10
y
=
y
/
10
p
=
my_math
.
product
(
x
,
y
)
self
.
assertEqual
(
p
,
x
*
y
,
'integer multiplication failed'
)
if
__name__
==
'__main__'
:
unittest
.
main
(
)
函數unittest.main負責替你運行測試:在測試方法前執行setUp方法,示例化所有的TestCase子類,并運行所有名稱以test打頭的方法。assertEqual方法檢車指定的條件(這里是相等),以判斷指定的測試是成功了還是失敗了。
??接著,我們運行前面的測試,輸出的結果如下:
begin
test
.begin
test
.
----------------------------------------------------------------------
Ran 2 tests
in
0.001s
OK
可以看到,該程序運行了兩個測試,每個測試前都會輸出’begin test’,
.
表示測試成功,若測試失敗,則返回的是
F
。
??接著模擬測試出錯的情形,將my_math函數中的product方法改成返回:
return
x
+
y
再運行測試腳本,輸出的結果如下:
begin
test
Fbegin
test
F
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
FAIL: test_floats
(
__main__.ProductTestcase
)
----------------------------------------------------------------------
Traceback
(
most recent call last
)
:
File
"test_my_math.py"
, line 20,
in
test_floats
self.assertEqual
(
p, x * y,
'integer multiplication failed'
)
AssertionError: -2.0
!=
1.0
:
integer multiplication failed
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
FAIL: test_integers
(
__main__.ProductTestcase
)
----------------------------------------------------------------------
Traceback
(
most recent call last
)
:
File
"test_my_math.py"
, line 12,
in
test_integers
self.assertEqual
(
p, x*y,
'integer multiplication failed'
)
AssertionError: -20
!=
100
:
integer multiplication failed
----------------------------------------------------------------------
Ran 2 tests
in
0.001s
FAILED
(
failures
=
2
)
兩條測試都未通過,返回的是
F
,并幫助你指出了錯誤的地方,接下來,你應該能快速地修復這個bug。
??關于unittest模塊的更加詳細的說明,可以參考網址: https://docs.python.org/3/library/unittest.html 。
總結
??本文介紹了兩個Python中的測試工具: doctest和unittest,并配以簡單的例子來說明這兩個測試模塊的使用方法,希望能對讀者有所幫助~
注意:不妨了解下筆者的微信公眾號: Python爬蟲與算法(微信號為:easy_web_scrape), 歡迎大家關注~
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
