鱼C论坛

 找回密码
 立即注册
查看: 1680|回复: 0

[技术交流] 标准库:doctest

[复制链接]
发表于 2022-3-6 16:05:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x


doctest 模块寻找像Python交互式代码的文本,然后执行这些代码来确保它们的确就像展示的那样正确运行,有许多方法来使用doctest:

  • 通过验证所有交互式示例仍然按照记录的方式工作,以此来检查模块的文档字符串是否是最新的。
  • 通过验证来自一个测试文件或一个测试对象的交互式示例按预期工作,来进行回归测试。
  • 为一个包写指导性的文档,用输入输出的例子来说明。 取决于是强调例子还是说明性的文字,这有一种 "文本测试 "或 "可执行文档 "的风格。


简单来说,就是执行注释中的代码!


下面是一个小却完整的示例模块:
  1. """
  2. This is the "example" module.

  3. The example module supplies one function, factorial().  For example,

  4. >>> factorial(5)
  5. 120
  6. """

  7. def factorial(n):
  8.     """Return the factorial of n, an exact integer >= 0.

  9.     >>> [factorial(n) for n in range(6)]
  10.     [1, 1, 2, 6, 24, 120]
  11.     >>> factorial(30)
  12.     265252859812191058636308480000000
  13.     >>> factorial(-1)
  14.     Traceback (most recent call last):
  15.         ...
  16.     ValueError: n must be >= 0

  17.     Factorials of floats are OK, but the float must be an exact integer:
  18.     >>> factorial(30.1)
  19.     Traceback (most recent call last):
  20.         ...
  21.     ValueError: n must be exact integer
  22.     >>> factorial(30.0)
  23.     265252859812191058636308480000000

  24.     It must also not be ridiculously large:
  25.     >>> factorial(1e100)
  26.     Traceback (most recent call last):
  27.         ...
  28.     OverflowError: n too large
  29.     """

  30.     import math
  31.     if not n >= 0:
  32.         raise ValueError("n must be >= 0")
  33.     if math.floor(n) != n:
  34.         raise ValueError("n must be exact integer")
  35.     if n+1 == n:  # catch a value like 1e300
  36.         raise OverflowError("n too large")
  37.     result = 1
  38.     factor = 2
  39.     while factor <= n:
  40.         result *= factor
  41.         factor += 1
  42.     return result


  43. if __name__ == "__main__":
  44.     import doctest
  45.     doctest.testmod()
复制代码


命令行执行:
  1. python example.py -v
复制代码

输出:
  1. Trying:
  2.     factorial(5)
  3. Expecting:
  4.     120
  5. ok
  6. Trying:
  7.     [factorial(n) for n in range(6)]
  8. Expecting:
  9.     [1, 1, 2, 6, 24, 120]
  10. ok
  11. Trying:
  12.     factorial(30)
  13. Expecting:
  14.     265252859812191058636308480000000
  15. ok
  16. Trying:
  17.     factorial(-1)
  18. Expecting:
  19.     Traceback (most recent call last):
  20.         ...
  21.     ValueError: n must be >= 0
  22. ok
  23. Trying:
  24.     factorial(30.1)
  25. Expecting:
  26.     Traceback (most recent call last):
  27.         ...
  28.     ValueError: n must be exact integer
  29. ok
  30. Trying:
  31.     factorial(30.0)
  32. Expecting:
  33.     265252859812191058636308480000000
  34. ok
  35. Trying:
  36.     factorial(1e100)
  37. Expecting:
  38.     Traceback (most recent call last):
  39.         ...
  40.     OverflowError: n too large
  41. ok
  42. 2 items passed all tests:
  43.    1 tests in __main__
  44.    6 tests in __main__.factorial
  45. 7 tests in 2 items.
  46. 7 passed and 0 failed.
  47. Test passed.
复制代码


原文出处:https://docs.python.org/zh-cn/3/library/doctest.html




本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 20:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表