1 # content of test_doctest.py 2 def something(): 3 """ a doctest in a docstring 4 >>> something() 5 42 6 """ 7 return 42 8 9 def num(n): 10 """ 11 function:斐波那契数列 12 >>> num(5) 13 [0, 1, 1, 2, 3] 14 """ 15 titles = [] 16 a = 0 17 b = 1 18 for i in range(n): 19 titles.append(a) 20 a, b = b, a+b 21 22 return titles 23 24 25 if __name__ == ‘__main__‘: 26 import doctest 27 doctest.testmod(verbose=True)
结果:
1 C:\Users\Administrator\PycharmProjects\untitled2\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/untitled2/forpytest/test_doctest.py 2 Trying: 3 num(5) 4 Expecting: 5 [0, 1, 1, 2, 3] 6 ok 7 Trying: 8 something() 9 Expecting: 10 42 11 ok 12 1 items had no tests: 13 __main__ 14 2 items passed all tests: 15 1 tests in __main__.num 16 1 tests in __main__.something 17 2 tests in 3 items. 18 2 passed and 0 failed. 19 Test passed. 20 21 Process finished with exit code 0
原文:https://www.cnblogs.com/for-master/p/13475595.html