[89.程序调试与测试(9):文档测试] 零基础学python,简单粗暴

概述

  • 文档测试是一种简单粗暴的测试方式;
  • 文档指的就是Python中的模块,文档测试就是对一个PY文件进行整体的测试;
  • 文档测试中的测试代码是以注释的形式写在文档中;
  • 通过标准库API来触发文档测试:doctest.testmod(target_module);

待测试的模块

"""
直接在模块的注释中声明要测试的函数、参数、结果预期

测试pow2()函数
>>> pow2(1)
1
>>> pow2(2)
4

测试pow3函数
>>> pow3(2)
8
"""

def pow2(x):
    return x ** 2

# 这个函数的逻辑是错误的
def pow3(x):
    return x ** 3 - 1

对目标模块进行文档测试

#引入文档测试模块
import doctest

# 引入要进行测试的目标模块mytest
from chapter10_test import mytest

if __name__ == '__main__':

    # 对mytest这个模块进行文档测试
    doctest.testmod(mytest)

测试结果 

结果显示:pow3(2)的结果预期为8实际得到7,3项测试中的1项失败了;


版权声明:本文为博主原创文章,未经博主允许不得转载.https://my.csdn.net/pangzhaowen

概述

  • 文档测试是一种简单粗暴的测试方式;
  • 文档指的就是Python中的模块,文档测试就是对一个PY文件进行整体的测试;
  • 文档测试中的测试代码是以注释的形式写在文档中;
  • 通过标准库API来触发文档测试:doctest.testmod(target_module);

待测试的模块

"""
直接在模块的注释中声明要测试的函数、参数、结果预期

测试pow2()函数
>>> pow2(1)
1
>>> pow2(2)
4

测试pow3函数
>>> pow3(2)
8
"""

def pow2(x):
    return x ** 2

# 这个函数的逻辑是错误的
def pow3(x):
    return x ** 3 - 1

对目标模块进行文档测试

#引入文档测试模块
import doctest

# 引入要进行测试的目标模块mytest
from chapter10_test import mytest

if __name__ == '__main__':

    # 对mytest这个模块进行文档测试
    doctest.testmod(mytest)

测试结果 

结果显示:pow3(2)的结果预期为8实际得到7,3项测试中的1项失败了;


猜你喜欢

转载自blog.csdn.net/pangzhaowen/article/details/80910417