Python测试模块

Python测试模块

doctest

介绍

doctest是python官方提供的文档测试模块,它可以对函数或者对象进行单元测试

简单的demo //函数测试代码

先来一个简单的测试模块

import doctest
def fun():
    '''
    >>> print(1)  #目标测试代码
    2  #预期结果
    '''
doctest.testmod() #执行这个测试案例
**********************************************************************
File "c:\Users\cpj6209\Documents\GitLibrary\test\book1_t1.py", line 38, in __main__.fun
Failed example:
    print(1)  
Expected:
    2
Got:
    1
**********************************************************************
1 items had failures:
   1 of   1 in __main__.fun
***Test Failed*** 1 failures.

在doctest中我们可以在函数或类中以多行注解的方式写测试代码。
函数doctest.testmod()会将每个函数或类中的开头注释运行。然后将与预期结果不同的语句打印出来,如上测试代码可以与本体没有任何联系。
其结构如下

>>> python 代码
预期执行结果
**********************************************************************
File "c:\Users\cpj6209\Documents\GitLibrary\test\book1_t1.py", line 38, in __main__.fun
Failed example:
    print(1)  //出现结果不同的代码
Expected:
    2    //预期的结果
Got:
    1    //实际的结果
**********************************************************************
1 items had failures:    //失败测试数量
   1 of   1 in __main__.fun  //失败的测试目标
***Test Failed*** 1 failures.
类测试代码

在上面我们已经写了函数的测试代码,这里写出类的测试代码

import doctest
class Obj:
    '''
    >>> print(10)
    4
    '''
doctest.testmod()
**********************************************************************
File "c:\Users\cpj6209\Documents\GitLibrary\test\book1_t1.py", line 44, in __main__.Obj
Failed example:
    print(10)
Expected:
    4
Got:
    10
**********************************************************************
1 items had failures:
   1 of   1 in __main__.Obj
***Test Failed*** 1 failures.

如上类的测试代码和函数的也没有什么区别

方法的测试代码
class Obj:
    '''   #类的测试
    >>> print(10)
    4
    '''
    a = 3
    def __init__(self):
        self.a = 10
        
    def fun():
        '''  #方法测试
        >>> print(33)
        334
        '''
        
    def test(self,c):
        '''
        >>> t = Obj()
        >>> t.test(4)
        3
        '''
        print(c,self.a)
    b = 3
    '''  #无效,会被认为是普通注释
    >>> print(44)
    523
    '''
doctest.testmod()
**********************************************************************
File "c:\Users\cpj6209\Documents\GitLibrary\test\book1_t1.py", line 60, in __main__.Obj
Failed example:
    print(10)
Expected:
    4
Got:
    10
**********************************************************************
File "c:\Users\cpj6209\Documents\GitLibrary\test\book1_t1.py", line 69, in __main__.Obj.fun
Failed example:
    print(33)
Expected:
    334
Got:
    33
**********************************************************************
File "c:\Users\cpj6209\Documents\GitLibrary\test\book1_t1.py", line 76, in __main__.Obj.test
Failed example:
    t.test(4)
Expected:
    3
Got:
    4 10
**********************************************************************
3 items had failures:
   1 of   1 in __main__.Obj
   1 of   1 in __main__.Obj.fun
   1 of   2 in __main__.Obj.test
***Test Failed*** 3 failures.

如上方法和函数的测试也没什么区别,单成员变量的注释是不会起作用的。这会被当做普通注释

更多特性

单个测试可以写多测试案例,且每个测试支持多行代码

def fff():
    '''
    >>> a = 34  #案例1  #支持多行
    >>> print(a)
    3
    >>> print(23)  #案例2
    4
    '''

doctest.testmod()
其他

由于测试内容可以和宿主没有关系,所以可以将测试代码全都写到独立文件中

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值