python中的单元测试可以使用doctest,unittest完成
1.doctest的使用
(1)写入程序如下(cubetest.py):
#!/usr/bin/python def cube(x): """ cube a number and return the result >>> cube(2) 8 >>> cube(3) 27 >>> cube(4) 64 """ return x**3; if __name__=='__main__': import doctest; doctest.testmod(verbose=True); |
测试用例写在字符串””” cube a number… “””中。
调用doctest中的testmod的方法即可测试。
输出如下:
Trying: cube(2) Expecting: 8 ok Trying: cube(3) Expecting: 27 ok Trying: cube(4) Expecting: 64 ok 1 itemshad no tests: __main__ 1 itemspassed all tests: 3 tests in __main__.cube 3 testsin 2 items. 3 passedand 0 failed. Testpassed. TestResults(failed=0, attempted=3) |