编程语言
首页 > 编程语言> > python+unittest日志和报告输出模块

python+unittest日志和报告输出模块

作者:互联网

1、日志输出模块:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # @Time    : 2019/11/21 17:41
 4 # @Site    : 
 5 # @File    : log.py
 6 # @Software: PyCharm
 7 
 8 import time
 9 import logging
10 
11 class logOutput():
12     def logOutput(self,log_dir,name_project):
13         '''
14         :param log_dir: 日志路径
15         :param name_project: 项目名称=>用于日志命名
16         :return:
17         '''
18         # sys.path.append(os.chdir('../log'))
19         now = time.strftime("%Y_%m_%d %H_%M_%S")
20         logging.basicConfig(level=logging.DEBUG,
21                             format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
22                             datefmt='%a, %d %b %Y %H:%M:%S',
23                             filename=log_dir+ now +'-'+name_project+'_test_log.log',
24                             filemode='w')
25         logger = logging.getLogger()
26         logger.info(self)

2、报告输出模块:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # @Time    : 2019/11/21 17:44
 4 # @Site    : 
 5 # @File    : report.py
 6 # @Software: PyCharm
 7 
 8 import time
 9 import unittest
10 from BSTestRunner import BSTestRunner
11 
12 class reportOutput():
13     def reportOutput(self,test_dir,report_dir,name_project):
14         '''
15         :param test_dir: 用例路径
16         :param report_dir: 报告路径
17         :param name_project: 项目名称=>用于报告命名及描述
18         :return:
19         '''
20         now = time.strftime("%Y_%m_%d %H_%M_%S")
21         discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')      #加载测试用例
22         report_name = report_dir + now + '-'+ name_project+'_test_report.html'    #报告名称
23         with open(report_name,'wb') as f:   ##运行用例生成测试报告
24             runner = BSTestRunner(stream=f,
25                                   title=name_project+' UIAuto_Regression Testing Report',
26                                   description=(name_project+U"UI自动化功能回归测试"),
27                                   verbosity=2)
28             runner.run(discover)
29             f.close()

3、调用方法及输出文件:

调用方法:

 

输出样式

 

 

标签:name,python,unittest,输出模块,project,report,_%,dir,log
来源: https://www.cnblogs.com/determined/p/11987232.html