Python文檔測試---doctest


Python中的文檔測試doctest非常之簡單,就是對注釋部分測試,按Python的自帶IDE的語法進行,即交互模式

下面看一個簡單的栗子,test.py

#coding:UTF-8
import doctest
def ceshi(x):
    """#這是文檔測試的內容
    >>> ceshi(2)
    1
    >>> ceshi(3)
    0
    """#End
    if x%2==0:
        return 1
    else:
        return 0

if __name__ == "__main__":
doctest.testmod(verbose=True)
#doctest.testmod是測試模塊,verbose默認是False,意思是出錯才用提示;True,對錯都有執行結果

 下面是運行結果:

 1 Trying:
 2     ceshi(2)
 3 Expecting:
 4     1
 5 ok
 6 Trying:
 7     ceshi(3)
 8 Expecting:
 9     0
10 ok
11 1 items had no tests:
12     __main__
13 1 items passed all tests:
14    2 tests in __main__.ceshi
15 2 tests in 2 items.
16 2 passed and 0 failed.
17 Test passed.

我們可以從第16行代碼看到,2個測試都通過了

文檔測試算是比較簡單的,要注意的地方就是格式上問題

>>>def ... 
#顯然是錯的

>>> def ...
#用過IDE的應該都知道,要空一格

好了,本章到此結束

End

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM