import unittest
import os
import HTMLTestRunner
import time
def allTestCase():
suite=unittest.TestLoader().discover(
start_dir=os.path.dirname(__file__),
pattern='test_*.py',
top_level_dir=None)
return suite
def getNowTime():
'''获取当前时间'''
return time.strftime('%Y-%m-%d %H_%M_%S')
def run():
'''生成测试报告'''
fp=open(getNowTime()+'testReport.html','wb')
HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='测试报告' ,
description='基于python自动化的测试实践').run(allTestCase())
if __name__== '__main__':
run()
如上图,生成的测试报告名称中带上了当前时间。