Python中的文档测试doctest非常之简单,就是对注释部分测试,按Python的自带IDE的语法进行,即交互模式
#doctest.testmod是测试模块,verbose默认是False,意思是出错才用提示;True,对错都有执行结果
举例说明:
def abs(n):
'''
Function to get absolute value of number.
Example:
>>> abs(1)
1
>>> abs(-1)
1
>>> abs(0)
0
'''
return n if n >= 0 else (-n)
#print(abs(-9))
if __name__ == "__main__":
doctest.testmod(verbose=True)
运行结果:
Trying:
abs(1)
Expecting:
1
ok
Trying:
abs(-1)
Expecting:
1
ok
Trying:
abs(0)
Expecting:
0
ok
1 items had no tests:
__main__
1 items passed all tests:
3 tests in __main__.abs
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
如果verbose=False,结果会怎样呢? 结果没有任何内容;