欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 自动化代码生成html 报告

程序员文章站 2022-05-14 18:54:03
...

先介绍比较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()