pythonunittest模块_python模块:doctest,unitest模块

1.使用doctest模块测试代码

[root@node1 tmp]# vim qwe.py

#!/bin/env python

#!-*- coding:UTF-8 -*-

def abc():

print 4;

[root@node1 tmp]# vim doctest.py

#!/bin/env python

#!-*- coding:UTF-8 -*-

import doctest,qwe             #加载doctest模块,加载qwe文件名

if __name__=="__main__":

doctest.testmod(qwe)

[root@node1 tmp]# python doctest.py           #检查python语法

[root@node1 tmp]# python doctest.py -v       #查看代码的执行过程

2 items had no tests:

qwe

qwe.abc

0 tests in 2 items.

0 passed and 0 failed.

Test passed.

[root@node1 tmp]#

2.使用unittest模块测试代码

unittest.main()函数负责实际运行测试.它会实例化所有TestCase的子类,运行所有名字以test方法的方法.

[root@node1 tmp]# vim unittest.py

#!/bin/env python

#!-*- coding:UTF-8 -*-

import unittest

class tong(unittest.TestCase):        #继承unittest模块

def testa(self):             #函数名必须test开头

print 'a'

def testb(self):           #函数名必须test开头

print 'b'

if __name__=="__main__":

unittest.main()          #检查所有以TestCase类,test开头的函数是否有异常

[root@node1 tmp]# python doctest1.py

a

.b

.

----------------------------------------------------------------------

Ran 2 tests in 0.000s

OK

[root@node1 tmp]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值