《最简洁的nose生成html报告方法》
安装:
nose是继承于unittest的一个python单元测试框架。
属于第三方包,要使用,首先需要安装,推荐pip(解决依赖):
pip install nose
pip install nose-html-reporting
注:命令行nosetests -help查看nose相关参数,证明安装成功。若pip安装失败,那就去安装whl吧
使用:
进入测试文件所在目录,或直接跟绝对路径(如:d:\test\test.py)
指定报告名称和路径使用--html-report参数(当然也可以DIY哈)
命令行执行:
nosetests d:\test\test.py --with-html --html-report=d:\test\test.html
注:解决有中文的输出问题需要修改nose_html_reporting的__init__.py
加入万能编码解决方案:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
然后再修改输出方式_format_output:
return o.decode('utf-8')
#return o.decode('latin-1')
《最简洁unittest生成测试报告》
使用HTMLTestRunner来格式化报告。下载HTMLTestRunner.py(容易搜索到)并放到python:\lib\路径下。
在测试py的加上下一段代码(更改类名哟,当然可以个性化DIY你自己的报告名称,比如添加文件名字、时间戳等)
总之最简洁的unittest生成测试报告,补充代码如下:
if __name__=='__main__':
testSuite=unittest.TestLoader().loadTestsFromTestCase(测试类名)
while True:
report_path='d:\\test\\'
filename=reportp_path+'result.html'
if os.path.exists(report_path):
with open(filename,'wb') as f:
runner=HTMLTestRunner.HTMLTestRunner(f,verbosity=2,title='Result',description='Test_reports')
runner.run(testSuite)
break
else:
os.makedirs(report_path)
continue
一切就绪之后执行你的unittest就ok。去见证一下吧。