doctest模块是内置模块
应用举例
# -*- coding: utf-8 -*- def add(x, y): """ 求和 x + y Args: x: int y: int Returns: int eg: >>> add(1, 1) 2 >>> add(5, 5) 10 >>> 2/0 Traceback (most recent call last): ... ZeroDivisionError: division by zero """ return x + y * 2 if __name__ == '__main__': import doctest doctest.testmod()
运行结果
********************************************************************** Failed example: add(1, 1) Expected: 2 Got: 3 ********************************************************************** Failed example: add(5, 5) Expected: 10 Got: 15 ********************************************************************** 1 items had failures: 2 of 2 in __main__.add ***Test Failed*** 2 failures. Process finished with exit code 0
如果所有测试用例都能通过测试,则不生成任何输出结果