如何自动运行tempest测试并自动输出html格式的测试报告。本文将会介绍写的脚本来完成。该脚本主要使用了HTMLTestRunner库。
具体代码如下:
#!/usr/bin/env python
#coding=utf-8
import HTMLTestRunner
import unittest,time
import re,os,sys
import time
def createsuite():
testunit=unittest.TestSuite()
test_dir='/root/zhoubin/tempest/tempest/scenario' #定义测试文件查找的目录
discover=unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py',top_level_dir=None)#测试scenario目录下的以test开头的测试用例
#discover方法筛选出来的测试用例,循环添加到测试套件中
for test_case in discover:
#print test_case
testunit.addTests(test_case)
return testunit
if __name__ == '__main__':
now =time.strftime("%Y-%m-%d-%H:%M:%S")
#测试报告存放路径
filename = '/root/zhoubin/tempest_result/'+now+'_tempest_neutron场景测试结果.html'
fp = file(filename,'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,title=u'tempest-neutron场景测试',description=u'测试结果:')
alltestnames = createsuite()
runner.run(alltestnames) #运行所有测试
fp.close()
该脚本会运行找到的所有测试用例,并将结果保存成html形式。见下图: