diff options
-rw-r--r-- | plugins/agl_test_base.py | 106 | ||||
-rw-r--r-- | plugins/agl_test_conf.py | 6 | ||||
-rw-r--r-- | plugins/agl_test_log.py | 19 | ||||
-rw-r--r-- | plugins/agl_test_report.py | 138 | ||||
-rw-r--r-- | template/summary_result_tpl.html | 18 |
5 files changed, 275 insertions, 12 deletions
diff --git a/plugins/agl_test_base.py b/plugins/agl_test_base.py new file mode 100644 index 0000000..6888d34 --- /dev/null +++ b/plugins/agl_test_base.py @@ -0,0 +1,106 @@ +import pytest + +import plugins.agl_test_utils as utils +import plugins.agl_test_conf as conf +import plugins.agl_test_log as log +import plugins.agl_test_report as report + +class AGLBaseTest: + + name: str + case_info_list = dict() + + def __init__(self, name: str): + self.name=name + utils.create_dir(self.name) + + def get_name(self): + return self.name + + def get_temp_logfile(self): + return conf.get_log_file(self.name) + + def get_logfile(self): + return conf.get_log_file(self.name) + + def get_workdir(self): + return conf.WORK_DIR + self.name + "/resource/" + + def append_one_caseinfo(name, value, status): + self.case_info_list[name] = [name, value, status] + + def get_all_caseinfo(self): + return self.case_info_list + + def get_caseinfo_by_name(self, name): + return self.case_info_list[name] + + def update_caseinfo_by_name(self, name, case_info): + self.case_info_list[name] = case_info + + def log_process(self): + logfile = self.get_logfile() + self.case_info_list = log.log_process(logfile) + self.init_case_status() + + def init_case_status(self): + for key in self.case_info_list: + case_info = self.case_info_list[key] + case_info[2] = "skipped" + #if (case_info[1] == "TEST-PASS"): + # case_info[2] = "passed" + #if (case_info[1] == "TEST-FAIL"): + # case_info[2] = "failed" + #if (case_info[1] == "TEST-SKIP"): + # case_info[2] = "skipped" + self.update_caseinfo_by_name(key, case_info) + + def log_report_json(self): + #Get case status list + #case_status format + # { + # 'test_id': 'status', + # 'test_id': 'status' + # } + case_status = report.format_caselist(self.case_info_list) + + #Get the summary status of the test set. + #summary format + #{ + # 'collected': collected_num, + # 'passed': passed_num, + # 'failed': failed_num, + # 'skipped": skipped_num + # } + summary = report.format_summary(self.case_info_list) + + #Get test set status + test_set_status = self.get_test_set_status(summary) + #Format data for json log + data = report.format_json_data(self.name, test_set_status, summary, case_status) + report.write_data_to_file(data) + + #Get test set status + #default output: + # passed: there is no failed case + # failed: there is one or more failed case + # skipped: all case is skipped or no case is run + def get_test_set_status(self, summary): + #Judge whether the test set passes + test_set_status = None + if (summary["collected"] == summary["skipped"]): + test_set_status = "skipped" + elif (summary["failed"] == 0): + test_set_status = "passed" + else: + test_set_status = "failed" + return test_set_status + + def log_report(self): + self.log_report_json() + #Package log file + report.log_compress(self.name) + + #Write json data to html + report.change_json_to_html(self.name) + diff --git a/plugins/agl_test_conf.py b/plugins/agl_test_conf.py index afffa68..d1178eb 100644 --- a/plugins/agl_test_conf.py +++ b/plugins/agl_test_conf.py @@ -25,6 +25,12 @@ TPL_DIR = BASE_DIR + "template/" #Defalut template file name for html result HTML_TPL = "summary_result_tpl.html" +#Color for html format +BGCOLOR_DARK = "CCCBE4" +BGCOLOR_LIGHT = "E8E7F2" +BGCOLOR_TABLE_TITLE = "2400B0" +COLOR_TABLE_TITLE = "white" + #Get the log file for a test set : # /var/run/agl-test/logs/tmp-log/${test_set_name}/log/${test_set_name}.log def get_log_file(test_set_name): diff --git a/plugins/agl_test_log.py b/plugins/agl_test_log.py index f7a0721..e064047 100644 --- a/plugins/agl_test_log.py +++ b/plugins/agl_test_log.py @@ -24,6 +24,25 @@ def log_process_default(log): return test_cases_values_and_status ''' +Process the log and init test_cases_values_and_status. + +log : the path of default log + +default log formate : + -> case_name: TEST-PASS + -> case_name: TEST-FAIL + -> case_name: TEST-SKIP +''' +def log_process(log): + pattern = '^ -> (.+?): (.+?)$' + parse_result = log_parse(log, pattern) + case_list = dict() + if parse_result: + for item in parse_result: + case_list[item[0]] = [item[0], item[1], ""] + return case_list + +''' Process the log create by gnome_desktop_testing and init test_cases_values_and_status. diff --git a/plugins/agl_test_report.py b/plugins/agl_test_report.py index 19c6dd7..8c7a514 100644 --- a/plugins/agl_test_report.py +++ b/plugins/agl_test_report.py @@ -1,14 +1,12 @@ import json import shutil -from plugins.agl_test_conf import REPORT_LOGS_DIR -from plugins.agl_test_conf import TMP_LOGS_DIR +import plugins.agl_test_conf as conf - -#Compress the tmp log to .zip, and store the zip file under TMP_LOGS_DIR/test-report +#Compress the tmp log to .zip def log_compress(THIS_TEST): - base_name = TMP_LOGS_DIR + "test-report/" + THIS_TEST + "/log" - root_dir = TMP_LOGS_DIR + THIS_TEST + "/log" + base_name = conf.get_log_dir(THIS_TEST) + root_dir = conf.get_tmp_log_dir(THIS_TEST) shutil.make_archive(base_name,'zip',root_dir) @@ -26,6 +24,22 @@ def get_case_status(test_cases_values_and_status): case_status[test_cases_values_and_status[i][0]] = test_cases_values_and_status[i][2] return case_status +#Input format +# { +# "test_id": ['test_id', 'values', 'status'], +# "test_id": ['test_id', 'values', 'status'] +# } +#Output format +# { +# 'test_id': 'status', +# 'test_id': 'status' +# } +def format_caselist(case_info_list: dict): + case_status = dict() + for key in case_info_list: + case_info = case_info_list[key] + case_status[key] = case_info[2] + return case_status #Case_status is a dictionary type of data,Record the test name/id and final results of all test cases #Get the summary of the test case status, the result is like that: @@ -43,8 +57,44 @@ def get_summary(case_status): summary = [["collected",collected_num],["passed",passed_num],["failed",failed_num],["skipped",skipped_num]] return summary +#Input format +# { +# "test_id": ['test_id', 'values', 'status'], +# "test_id": ['test_id', 'values', 'status'] +# } +#Output format +# { +# 'collected': collected_num, +# 'passed': passed_num, +# 'failed': failed_num, +# 'skipped": skipped_num +# } +def format_summary(case_info_list): + collected_num = passed_num = failed_num = skipped_num = 0 + for key in case_info_list: + case_info = case_info_list[key] + collected_num = collected_num + 1 + if (case_info[2] == "passed"): + passed_num = passed_num + 1 + elif (case_info[2] == "failed"): + failed_num = failed_num + 1 + else: + skipped_num = skipped_num + 1 + summary = dict() + summary["collected"] = collected_num + summary["passed"] = passed_num + summary["failed"] = failed_num + summary["skipped"] = skipped_num + return summary + +def format_json_data(name, test_set_status, summary, case_status): + data = summary + data["test_name"] = name + data["test_status"] = test_set_status + data["case_status"] = case_status + return summary -#Write the test result to a json file under the dir TMP_LOGS_DIR +#Write the test result to a json file def write_date_to_json(THIS_TEST,test_set_status,summary,case_status): #The data that will be written into the json file data = { @@ -58,10 +108,7 @@ def write_date_to_json(THIS_TEST,test_set_status,summary,case_status): } #Write the "data" to the json file - report_json = TMP_LOGS_DIR + THIS_TEST + "/" + "report.json" - with open(report_json,'w') as f: - json.dump(data,f,indent=4,sort_keys=False) - f.close() + write_data_to_file(data, "json") def get_report_html(THIS_TEST,test_set_status,summary,case_status): html = "<html>" @@ -114,7 +161,74 @@ def get_report_html(THIS_TEST,test_set_status,summary,case_status): return html def write_to_html_file(THIS_TEST,html): - html_path = TMP_LOGS_DIR + "test-report/" + THIS_TEST + "/report.html" + html_path = conf.get_html_filename(THIS_TEST) html_file = open(html_path,"w") html_file.write(html) html_file.close() + +def create_gen_web_page(filename, tlpname, html_data): + # here, jinja2 module should be installed first + from jinja2 import Environment,FileSystemLoader + env = Environment(loader=FileSystemLoader(conf.get_tpl_dir())) + template = env.get_template(tlpname) + with open(filename,'w+') as f: + html_content = template.render(data=html_data) + f.write(html_content) + f.close() + +def write_data_to_file(data, file_type = "json"): + print(data) + testname = data["test_name"] + if (file_type == "json"): + filename = conf.get_json_filename(testname) + with open(filename,'w') as f: + json.dump(data,f,indent=4,sort_keys=False) + f.close() + elif (file_type == "html"): + filename = conf.get_html_filename(testname) + tlpname = conf.get_default_html_tpl() + create_gen_web_page(filename, tlpname, data) + else: + #TODO waiting for adding + print("ERROR: file type %s is error. Expect json or html." % file_type) + +def change_json_to_html(test_set_name): + json_file = conf.get_json_filename(test_set_name) + html_file = conf.get_html_filename(test_set_name) + #read data from json file + html_data = dict() + with open(json_file,'r') as f: + html_data = json.load(f) + f.close() + + #format json data to html data + html_data["case_status_html"] = format_case_status_table(html_data["case_status"]) + + #save data to html file + write_data_to_file(html_data, "html") + +def format_case_status_table(case_status): + html_data = "" + #init table title + bgcolor = conf.BGCOLOR_TABLE_TITLE + color = conf.COLOR_TABLE_TITLE + html_data += "\t<tr bgcolor = \"" + bgcolor + "\">\n" + html_data += "\t\t<th><font color = \"" + color + "\"> test case </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> status </font></th>\n" + html_data += "\t</tr>\n" + + #init all rows + bgcolor = conf.BGCOLOR_DARK + for key in case_status: + if ( bgcolor == conf.BGCOLOR_DARK ): + html_data += "\t<tr bgcolor = \"" + bgcolor + "\">\n" + bgcolor = conf.BGCOLOR_LIGHT + else: + html_data += "\t<tr bgcolor = \"" + bgcolor + "\">\n" + bgcolor = conf.BGCOLOR_DARK + html_data += "\t\t<th>" + key + "</th>\n" + html_data += "\t\t<th>" + case_status[key] + "</th>\n" + html_data += "\t</tr>\n" + print(html_data) + return html_data + diff --git a/template/summary_result_tpl.html b/template/summary_result_tpl.html new file mode 100644 index 0000000..6b8f9fe --- /dev/null +++ b/template/summary_result_tpl.html @@ -0,0 +1,18 @@ +<!DOCTYPE html> +<html> + <head> + <title>{{ data.test_name }} test report</title> + </head> + <body> + <h1> {{ data.test_name }} test report </h1> + <li> Status : {{ data.test_status }} </li> + <li> Total : {{ data.collected }} Pass : {{ data.passed }} Fail : {{ data.failed }} Skip : {{ data.skipped }} </li> + <li> Details : </li> + <table border="1" cellspacing="2"> +{{ data.case_status_html }} + </table> + <p></p> + <li>Detail log : {{ data.test_name }}/log.zip</li> + </body> +</html> + |