Python测试模块doctest使用解析

yizhihongxing

Python测试模块doctest使用解析

在Python中,doctest是一个非常有用的测试工具,可以帮助我们测试函数和模块的正确性。本文将详细讲解Python测试模块doctest的用法和解析。

doctest的基本用法

doctest模块提供了一种简单的方式来测试Python代码的正确性,它可以从文档字符串中提取代码示例,并执行这些示例,然后比较输出结果和预期结果是否一致。以下是一个使用doctest模块测试函数的示例:

def add(a, b):
    """
    This function adds two numbers.

    >>> add(2, 3)
    5
    >>> add(5, 7)
    12
    """
    return a + b

if __name__ == '__main__':
    import doctest
    doctest.testmod()

在上面的代码中,我们定义了一个add()函数,并在函数的文档字符串中添加了两个示例。使用testmod()函数来执行测试,如果测试通过,则不会有任何输出,否则会输出错误信息。

doctest的高级用法

doctest模块提供了多种高级用法,包括自定义比较器、跳过测试、测试嵌套代码块等。以下是一些常用的高级用法:

1. 自定义比较器

使用自定义比较器可以控制如何比较输出结果和预期结果。以下是一个使用自定义比较器测试函数的示例:

```python
def add(a, b):
"""
This function adds two numbers.

>>> add(2, 3)
5
>>> add(5, 7)
12
>>> add(0.1, 0.2)
0.3
"""
return a + b

def compare_floats(x, y, eps=0.0001):
return abs(x - y) < eps

if name == 'main':
import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE,
verbose=True,
report=True,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,
optionflags=doctest.DONT_ACCEPT_TRUE_FOR_1,
optionflags=doctest.DONT_ACCEPT_BLANKLINE,
optionflags=doctest.NORMALIZE_FLOATS,
optionflags=doctest.ELLIPSIS,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORT_UDIFF,
optionflags=doctest.REPORT_CDIFF,
optionflags=doctest.REPORT_NDIFF,
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE,
optionflags=doctest.REPORTING_FLAGS,
optionflags=doctest.COMPARISON_FLAGS,
optionflags=doctest.IGNORE_EXCEPTION_DETAIL,
optionflags=doctest.FAIL_FAST,
optionflags=doctest.SKIP,
optionflags=doctest.NORMALIZE_WHITESPACE,
optionflags=doctest.IGNORE_UNICODE,

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python测试模块doctest使用解析 - Python技术站

(0)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • python实现将excel文件转化成CSV格式

    下面是Python实现将Excel文件转化成CSV格式的完整实例教程,包含了两条示例说明。 1. 准备工作 在开始之前,需要确保你的电脑上已经安装了Python以及以下几个Python库:* pandas:处理数据的主要库,可以用于读写Excel、CSV、数据库等格式的数据。* xlrd:用于读取Excel文件的Python库。* openpyxl:用于读取…

    python 2023年5月13日
    00
  • Python 获取命令行参数内容及参数个数的实例

    下面是详细的攻略: 1. 获取命令行参数内容及参数个数 在Python中,可以通过sys模块中的argv来获取命令行参数内容及参数个数。 1.1 获取参数个数 下面是一个获取参数个数的示例: import sys print("参数个数为:", len(sys.argv)) 其中,sys.argv返回的是一个列表,包含了所有的命令行参数,…

    python 2023年6月3日
    00
  • Python基础知识+结构+数据类型

    Python基础知识+结构+数据类型 本攻略旨在为初学者提供关于Python基础知识、结构和数据类型的全面指导,包括以下主题: Python基础知识 Python数据类型 Python流程控制语句 Python函数 1. Python基础知识 Python是一种解释型的高级编程语言,它的语法简单、可读性高、功能强大。首先了解Python的基本语法和一些编程概…

    python 2023年5月18日
    00
  • python中将\\uxxxx转换为Unicode字符串的方法

    在Python中,可以使用unicode_escape编解码器将\uxxxx表示的Unicode字符序列转换为Unicode字符串。下面是具体步骤: 定义一个包含\uxxxx表示的Unicode字符序列的字符串 例如,我们有一个字符串”\u4e2d\u6587″,表示中文两个字。这个字符串实际上是两个Unicode字符,分别对应中文的“中”和“文”。 使用u…

    python 2023年5月20日
    00
  • Python中使用Frozenset对象的案例详解

    标题: Python中使用Frozenset对象的案例详解 简介 在 Python 中,Frozenset 对象是一种不可变集合。与可变集合相比,Frozenset 对象的一个主要优点是可以用作其他集合类型的键,例如字典。 创建 Frozenset 对象 使用 frozenset() 函数可以创建一个新的 Frozenset 对象。Frozenset 对象可…

    python 2023年5月14日
    00
  • 详解如何用PIL将一个透明的PNG图像与另一个图像合并

    请看下面的攻略。 PIL将透明PNG图像与其他图像合并 1.解释PIL? Pillow 是 Python 图像处理库,可用于打开、操作及保存多种图像格式,包括但不限于 PNG、JPEG、BMP、GIF、WebP、PPM、TIFF。 2. 什么是透明PNG图片? PNG 图像文件格式支持透明度,这意味着您可以使用 PNG 图像作为遮罩或将其与其他图像合并,以使…

    python-answer 2023年3月25日
    00
  • 使用Requests库来进行爬虫的方式

    使用Requests库是Python中最为常用的网络爬虫方式之一。这个库非常易用,支持HTTP/HTTPS/FTP协议的请求和响应处理,同时还能够自定义请求头、cookie等参数,方便地实现网站的爬取。下面是使用Requests库来进行爬虫的完整攻略: 安装Requests库 使用pip命令安装Requests库: pip install requests …

    python 2023年5月14日
    00
  • 在Python中调用Ping命令,批量IP的方法

    以下是调用Ping命令批量IP的方法: 1. 通过subprocess模块调用Ping命令 subprocess模块提供了调用系统命令的功能,可以通过它来调用Ping命令。具体步骤如下: 从标准库中导入subprocess模块; 使用subprocess.Popen方法调用系统命令,传入参数,如序列类型的命令参数; 通过.communicate()方法来读取…

    python 2023年6月2日
    00
合作推广
合作推广
分享本页
返回顶部