python 自动化代码生成html 报告
先介绍比较low的方法一:
import unittest
from unittest import TestSuite,TextTestRunner
from HTMLTestRunner import HTMLTestRunner
class Test1(unittest.TestCase):
@classmethod
def setUpClass(cls):
print “preparing…”
@classmethod
def tearDownClass(cls):
print “shutdown…”
def test1(self):
print “testing1”
def test2(self):
print “testing2”
class Test2(unittest.TestCase):
@classmethod
def setUpClass(cls):
print “preparing…”
@classmethod
def tearDownClass(cls):
print “shutdown…”
def test1(self):
print “testing3”
def test2(self):
print “testing4”
if name==‘main’:
s=TestSuite()
s.addTest(Test1(‘test1’))
s.addTest(Test1(‘test2’))
s.addTest(Test2(‘test1’))
s.addTest(Test2(‘test2’))
t=TextTestRunner() # 调用执行套件方法
t.run(s) #执行套件
with open (‘d:\test11.html’,‘wb’)as f:
text=HTMLTestRunner(stream=f,title=u"测试报告",description=u"测试结果如下")
text.run(s)
效果
方法二:进阶调用discover进行寻找test开头所有用例文件一次批量完成测试套件添加
def all_cases():
case_path = os.getcwd()
discover = unittest.defaultTestLoader.discover(case_path, pattern='test*.py', top_level_dir=None)
return discover
def run(report_path):
with open(report_path, ‘wb’) as f:
runner = HTMLTestRunner(stream=f, title=“interface report”, description=“results like following:”, verbosity=2)
runner.run(all_cases())
f.close()
下一篇: JS正则表达式获取字符串中特定字符的方法