Interpret and process the beautifullreport report template

Keywords: Python pip Attribute Windows Pycharm

After the script using unittest framework is executed, an html report will be generated

This report makes an html template in advance, writes the corresponding content to the template, and generates a final report. This report template will exist in the following path after passing the PIP install beautiful report:

C: \ program files \ python37 \ lib \ site packages \ beautifullreport \ template. This html template can change some table attribute names to suit your own, for example:

 1 <body class="gray-bg">
 2 <div class="row  border-bottom white-bg dashboard-header">
 3     <div class="col-sm-12 text-center">
 4         <span style="${title}">Automated test report</span>
 5     </div>
 6 </div>
 7 <div class="wrapper wrapper-content animated fadeInRight">
 8     <div class="row">
 9         <div class="col-sm-12">
10             <div class="ibox float-e-margins">
11                 <div class="ibox-title">
12                     <h5 style="${sub-title}">Report summary</h5>
13                     <div class="ibox-tools">
14                         <a class="collapse-link">
15                             <i class="fa fa-chevron-up"></i>
16                         </a>
17                         <a class="close-link">
18                             <i class="fa fa-times"></i>
19                         </a>
20                     </div>
21                 </div>
22                 <div class="ibox-content">
23                     <div class="row">
24                         <div class="col-sm-6 b-r" style="height:350px">
25                             <form class="form-horizontal">
26                                 <div class="form-group">
27                                     <label class="col-sm-2 control-label text-info">Test purpose:</label>
28                                     <div class="col-sm-5">
29                                         <span class="form-control" id="testName"></span>
30                                     </div>
31                                 </div>
32                                 <div class="form-group">
33                                     <label class="col-sm-2 control-label text-info">Total use case:</label>
34                                     <div class="col-sm-5">
35                                         <span class="form-control" id="testAll"></span>
36                                     </div>
37                                 </div>
38                                 <div class="form-group">
39                                     <label class="col-sm-2 control-label text-navy">Use case pass:</label>
40                                     <div class="col-sm-5">
41                                         <span class="form-control" id="testPass"></span>
42                                     </div>
43                                 </div>
44                                 <div class="form-group">
45                                     <label class="col-sm-2 control-label text-danger">Use case failure:</label>
46                                     <div class="col-sm-5">
47                                         <span class="form-control text-danger" id="testFail"></span>
48                                     </div>
49                                 </div>
50                                 <div class="form-group">
51                                     <label class="col-sm-2 control-label text-warning">Use case skip:</label>
52                                     <div class="col-sm-5">
53                                         <span class="form-control text-warning" id="testSkip"></span>
54                                     </div>
55                                 </div>
56                                 <div class="form-group">
57                                     <label class="col-sm-2 control-label text-info">start time:</label>
58                                     <div class="col-sm-5">
59                                         <span class="form-control" id="beginTime"></span>
60                                     </div>
61                                 </div>
62                                 <div class="form-group">
63                                     <label class="col-sm-2 control-label text-info">Running time:</label>
64                                     <div class="col-sm-5">
65                                         <span class="form-control" id="totalTime"></span>
66                                     </div>
67                                 </div>
68                             </form>
69                         </div>
70                         <div class="col-sm-6">
71                             <div style="height:350px" id="echarts-map-chart"></div>
72                         </div>
73                     </div>
74                 </div>
75             </div>
76         </div>
77     </div>

 

So what can we do through this report generation, compare the specified storage path, report topics, etc., look directly at the BeautifulReport code, and find that when you want to instantiate this class, you must first import a suite (that is, the test case set), then call the class report method to generate the parameters that can be passed into when the report is generated: description, filename: STR = none, report  dir = '.', log  path = none, theme  theme  default '. After the log  path is removed, four parameters can be passed in. The specific usage of each parameter is detailed in the code, and will not be repeated here.

 

 1 class BeautifulReport(ReportTestResult, PATH):
 2     img_path = 'img/' if platform.system() != 'Windows' else 'img\\'
 3 
 4     def __init__(self, suites):
 5         super(BeautifulReport, self).__init__(suites)
 6         self.suites = suites
 7         self.report_dir = None
 8         self.title = 'Automated test report'
 9         self.filename = 'report.html'
10 
11     def report(self, description, filename: str = None, report_dir='.', log_path=None, theme='theme_default'):
12         """
13             Generate test report,And put it under the current running path
14         :param report_dir: generate report File storage path for
15         :param filename: File generated filename
16         :param description: Generate comments for files
17         :param theme: Report subject name theme_default theme_cyan theme_candy theme_memories
18         :return:
19         """
20         if log_path:
21             import warnings
22             message = ('"log_path" is deprecated, please replace with "report_dir"\n'
23                        "e.g. result.report(filename='Test report_demo', description='Test report', report_dir='report')")
24             warnings.warn(message)
25 
26         if filename:
27             self.filename = filename if filename.endswith('.html') else filename + '.html'
28 
29         if description:
30             self.title = description
31 
32         self.report_dir = os.path.abspath(report_dir)
33         os.makedirs(self.report_dir, exist_ok=True)
34         self.suites.run(result=self)
35         self.stopTestRun(self.title)
36         self.output_report(theme)
37         text = '\n Test completed, May open {} View report'.format(os.path.join(self.report_dir, self.filename))
38         print(text)

 

Here are the implementation methods of calling this module:

 1 # -*- coding:utf-8 -*-
 2 '''
 3 # @Time    : 2019/12/3 16:50
 4 # @Author  : nihaoya
 5 # @FileName: WeiBo_test.py
 6 # @Software: PyCharm
 7 '''
 8 import os
 9 import time
10 import unittest
11 from BeautifulReport import BeautifulReport as bf
12 
13 class WeiBo(unittest.TestCase):
14 Omit here
15 
16 if __name__ == "__main__":
17     suite = unittest.TestLoader().loadTestsFromTestCase(WeiBo)
18     run = bf(suite)
19     run.report(filename=u"Microblog test report_" + time.strftime("%Y~%m~%d %H~%M~%S"), description=u"Browse Weibo in the form of tourists", report_dir="report", theme="theme_memories")

Posted by herreram on Sun, 08 Dec 2019 19:58:41 -0800