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测试,显示


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

可以看到更加详细的信息。

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.

你可能感兴趣的:(python,doctest)