python doctest例子及注意事項

def s(x):
    '''
    >>> s(2) #<span style="color:#cc0000;">注意這裏必定要在>>>後面有空格</span>
    99
    >>> s(4)
    16
    '''
    return x*x

if __name__=='__main__':
    import doctest,mycs
    doctest.testmod(mycs)


在IDLE下編輯,點擊F5測試,顯示python


File "E:/Python_Study/code\mycs.py", line 3, in mycs.s
Failed example:
    s(2)
Expected:
    99
Got:
    4
**********************************************************************
1 items had failures:
   1 of   2 in mycs.s
***Test Failed*** 1 failures.






也能夠用cmd測試,配置完環境變量後,在相應目錄下輸入

python mycs.py -v測試

能夠看到更加詳細的信息。spa

e:\Python_Study\code>python mycs.py -v
Trying:
    s(2)
Expecting:
    99
**********************************************************************
File "e:\Python_Study\code\mycs.py", line 3, in mycs.s
Failed example:
    s(2)
Expected:
    99
Got:
    4
Trying:
    s(4)
Expecting:
    16
ok
1 items had no tests:
    mycs
**********************************************************************
1 items had failures:
   1 of   2 in mycs.s
2 tests in 2 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.
code

相關文章
相關標籤/搜索