diff options
author | 2022-05-26 13:06:34 +0800 | |
---|---|---|
committer | 2022-05-26 13:06:34 +0800 | |
commit | cb256cee0fe43430b461dcee392267dc65514e41 (patch) | |
tree | e8182b2c548f60cab6c4d614e92d67ad318a7c74 /plugins/agl_test_report.py | |
parent | bd0eb0ec729a228a317d2966e0f71770ca4dd36f (diff) |
agl-test-framework: add AGLBaseTest class
Add AGLBaseTest class for existing test suites which has own test scripts.
It package the default operations, like as log processing and file saving.
In test suite, please create a child class based onAGLBaseTest.
Then it is easy to init, run tests, check results and make a repoter.
Bug-AGL: SPEC-4345
Signed-off-by: Qiu Tingting <qiutt@fujitsu.com>
Change-Id: I9dbc01db59df16eb5718b19b3223ad95da0afb11
Diffstat (limited to 'plugins/agl_test_report.py')
-rw-r--r-- | plugins/agl_test_report.py | 138 |
1 files changed, 126 insertions, 12 deletions
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 + |