python除了unittest,还有nosetests,使用更快捷
nosetests的口号:
nose extends unittest to make testing easier
特点:
自动发现测试用例(包含[Tt]est文件以及文件包中包含test的函数)如:以test开头的文件;以test开头的函数或方法;以Test开头的类
nose自动收集单元测试,会自动识别源代码文件、目录或包中的测试用例,任何符合正则表达式:(?:^|[b_.-])[Tt]est的类、函数、文件或目录,以及TestCase的子类都会被识别并执行,匹配成功的包、任何python的源文件都会被当做测试用例。
1.安装
pip install nose
测试安装是否成功
nosetests -V
2.使用
常用命令
nosetests –h 查看所有nose相关命令 nosetests –s 执行并捕获输出 nosetests –with-xunit 输出xml结果报告 nosetests -v 查看nose的运行信息和调试信息 nosetests -w 目录:指定一个目录运行测试
(1)简单的测试用例
新建test.py
class Testclass: def __init__(self): pass def testfunc1(self): print ‘this is case1‘ def testfunc2(self): print ‘this is case2‘ def testfunc3(self): print ‘this is case3‘
执行
nosetests -s
执行
nosetests -v
(2)使用编码实现测试用例的执行
新建main.py
import nose result = nose.run() print(result)
执行
python main.py
原文:https://www.cnblogs.com/baby123/p/10901326.html