Python编程:使用doctest进行文档测试

网友投稿 453 2022-05-29

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()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Python编程:使用doctest进行文档测试

18

19

20

21

22

23

24

25

26

27

28

29

30

运行结果

********************************************************************** 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

如果所有测试用例都能通过测试,则不生成任何输出结果

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:word文档如何插入公式编号并对齐
下一篇:pycharm的docstring文档字符串风格
相关文章