1、如何收集测试结果?
使用第三方封装好类HTMLTestRunner.py生成HTML测试报告
# encoding:utf-8
import unittest
import time
from HTMLTestRunner import HTMLTestRunner
class MyTestCase(unittest.TestCase):
# 每条用例初始化
def setUp(self):
self.initdata = "hello allard"
# 测试用例,以test开头
def test_something(self):
self.assertEqual("hello allard", self.initdata)
# 每条用例执行完后释放资源
def tearDown(self):
pass
if __name__ == '__main__':
# 声明一个suite
suite = unittest.TestSuite()
# 从类加载用例集
cases = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
# 添加用例到suite
suite.addTests(cases)
# 声明TestRunner
# myTestRunner = unittest.TextTestRunner(verbosity=2)
# 生成HTML TestReport
fp = open("testreport.html", "wb")
myTestRunner = HTMLTestRunner(stream=fp, title=u"测试报告",
description=u"用例测试情况")
# 执行Runner
myTestRunner.run(suite)
# 关闭文件流
fp.close()
2、如何管理测试报告?
把所有执行的测试结果都收集起来,以时间戳作为报告的名称,存到某一个路径下就可以实现对Report管理。
# 将上面脚本中“生成HTML TestReport”代码改成如下:
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime())
HtmlFile = "report/" + now + "_Report.html"
fp = open(HtmlFile, "wb")
myTestRunner = HTMLTestRunner(stream=fp, title=u"测试报告",
description=u"用例测试情况")
# 执行Runner
myTestRunner.run(suite)
# 关闭文件流
另外一种方式
代码文件目录如下
test.py 代码内容
# coding:utf-8 __author__ = 'Jeff.xie' import importlib import unittest from time import sleep import sys,os sys.path.append(os.getcwd()) class Test(unittest.TestCase): @classmethod def setUpClass(self): print('start') def tearDownClass(self): print('finish') def test_1(self): print(1) self.assertEqual(2,2) def test_2(self): print(2) def test_3(self): print(3) assert 3==4 if __name__ == '__main__': suite =unittest.TestSuite() suite.addTest(Test('test_2')) suite.addTest(Test('test_1')) runner = unittest.TextTestRunner() runner.run(suite)
test1.py 代码内容
__author__ = 'Jeff.xie' import unittest from study.s_unittest.test import Test from study.s_unittest.HTMLTestRunner import HTMLTestRunner # import HTMLTestRunner.HTMLTestRunner as HTML import sys,os,time sys.path.append(os.getcwd()) class Test1(unittest.TestCase): def test_suite(self): now = time.strftime("") suit = unittest.TestSuite() list = ['test_2', 'test_1', 'test_3'] for aa in list: suit.addTest(Test(aa)) # f = open('report2.html', 'wb') # report = HTMLTestRunner(stream=f, title='title', description='desc') # report.run(suit) # %Y Year with century as a decimal number. # %m Month as a decimal number [01,12]. # %d Day of the month as a decimal number [01,31]. # %H Hour (24-hour clock) as a decimal number [00,23]. # %M Minute as a decimal number [00,59]. # %S Second as a decimal number [00,61]. # %z Time zone offset from UTC. # %a Locale's abbreviated weekday name. # %A Locale's full weekday name. # %b Locale's abbreviated month name. # %B Locale's full month name. # %c Locale's appropriate date and time representation. # %I Hour (12-hour clock) as a decimal number [01,12]. # %p Locale's equivalent of either AM or PM. now_str=time.strftime("%Y_%m_%d_%H_%M_%S",time.localtime(time.time())) reportname = 'report_'+now_str+'.html' with open(reportname, 'wb') as f: HTMLTestRunner(stream=f, title='title', description='desc', verbosity=2).run(suit) # runner = unittest.TextTestResult(verbosity=2) # runner.run(suit) if __name__ == '__main__': unittest.main()
自动生成了测试报告