diff options
-rw-r--r-- | conftest.py | 152 | ||||
-rw-r--r-- | template/all_test_suites_tpl.html | 17 |
2 files changed, 84 insertions, 85 deletions
diff --git a/conftest.py b/conftest.py index 512c1ba..0c42f66 100644 --- a/conftest.py +++ b/conftest.py @@ -3,35 +3,33 @@ import pytest import json import shutil import subprocess +from jinja2 import Environment,FileSystemLoader -from plugins.agl_test_conf import BASE_LOGS_DIR -from plugins.agl_test_conf import TMP_LOGS_DIR -from plugins.agl_test_conf import REPORT_LOGS_DIR -from plugins.agl_test_conf import TMP_TEST_REPORT +import plugins.agl_test_conf as conf @pytest.fixture(scope='session' ,autouse=True) def setup_compress_function(): #Before the test start, clean the env - report_json = TMP_LOGS_DIR + "report.json" + report_json = conf.TMP_LOGS_DIR + "report.json" output = subprocess.run(['ls',report_json],stdout=subprocess.PIPE,stderr=subprocess.PIPE) if(output.returncode == 0): subprocess.run(['rm',report_json]) #Makdir of TMP_TEST_REPORT and REPORT_LOGS_DIR - subprocess.run(['mkdir','-p',TMP_TEST_REPORT]) - subprocess.run(['mkdir','-p',REPORT_LOGS_DIR]) + subprocess.run(['mkdir','-p',conf.TMP_TEST_REPORT]) + subprocess.run(['mkdir','-p',conf.REPORT_LOGS_DIR]) yield #Collect report.json from all test sets to generate a report.json for all the test sets - report_files = TMP_LOGS_DIR + "report_files" + report_files = conf.TMP_LOGS_DIR + "report_files" with open(report_files,'w') as report_f: - subprocess.run(['find','-name','report.json'],cwd=TMP_LOGS_DIR,stdout=report_f) + subprocess.run(['find','-name','report.json'],cwd=conf.TMP_LOGS_DIR,stdout=report_f) report_f.close() #Get the summary data and write to report.json file summary_data = get_summary_data(report_files) - summary_json = TMP_LOGS_DIR + "/report.json" + summary_json = conf.TMP_LOGS_DIR + "/report.json" with open(summary_json, 'w') as summary_file: json.dump(summary_data,summary_file,indent=4,sort_keys=False) summary_file.close() @@ -44,20 +42,24 @@ def setup_compress_function(): zip_name = "agl-test-log-" + version + '-' + machine_name + '-' + date #Creat summary report in html - html = get_summary_report_html(summary_data,zip_name) - html_path = TMP_LOGS_DIR + "test-report/summary-report.html" - html_file = open(html_path,"w") - html_file.write(html) + summary_html_data = format_test_suite_table(summary_data) + summary_html_data["log_zip_name"] = zip_name + env = Environment(loader=FileSystemLoader(conf.get_tpl_dir())) + template = env.get_template("all_test_suites_tpl.html") + html_path = conf.TMP_LOGS_DIR + "test-report/summary-report.html" + with open(html_path, "w") as html_file: + html_content = template.render(data=summary_html_data) + html_file.write(html_content) html_file.close() #Copy summary report file - source_file = TMP_LOGS_DIR + "test-report/summary-report.html" - target_file = REPORT_LOGS_DIR + "summary-report.html" + source_file = conf.TMP_LOGS_DIR + "test-report/summary-report.html" + target_file = conf.REPORT_LOGS_DIR + "summary-report.html" shutil.copyfile(source_file,target_file) #Package the test report - base_name = REPORT_LOGS_DIR + zip_name - root_dir = TMP_LOGS_DIR + "test-report" + base_name = conf.REPORT_LOGS_DIR + zip_name + root_dir = conf.TMP_LOGS_DIR + "test-report" shutil.make_archive(base_name,"zip",root_dir) @@ -71,7 +73,7 @@ def get_summary_data(report_files): if not report: break report = report[1:-1] - report_json = TMP_LOGS_DIR + report + report_json = conf.TMP_LOGS_DIR + report with open(report_json,'r') as f: data = json.load(f) @@ -111,71 +113,51 @@ def get_summary_data(report_files): "summary_skipped": summary_skipped, } + status = "" + if (summary_data["summary"]["summary_total"] == summary_data["summary"]["summary_skipped"]): + status = "skip" + elif (summary_data["summary"]["summary_failed"] == 0): + status = "pass" + else: + status = "fail" + summary_data["summary"]["status"] = status + return summary_data -#Generate content for summary report json file -def get_summary_report_html(summary_data,zip_name): - status = "fail" - if(summary_data["summary"]["summary_total"]==summary_data["summary"]["summary_passed"]): - status = "success" - html = "<html>" - - #<head> </head> - html = html + "<head>" - html = html + "<title>" - html = html + "Summary Report" - html = html + "</title>" - html = html + "</head>" - - #<body> </body> - html = html + "<body>" - html = html + "<h1>" + "Summary Report" + "</h1>" - html = html + "<p>" + "Status :" + status + "</p>" - html = html + "<p>" + "Total: " + str(summary_data["summary"]["summary_total"]) - html = html + " Pass: " + str(summary_data["summary"]["summary_passed"]) - html = html + " Fail: " + str(summary_data["summary"]["summary_failed"]) - html = html + " Skip: " + str(summary_data["summary"]["summary_skipped"]) + "</p>" - html = html + "<p>Details : </p>" - - #<table> </table> - html = html + "<table border=\"1\" cellspacing=\"2\" >" - html = html + "<tr bgcolor = \"2400B0\">" - html = html + "<th><font color = \"white\">test suite</font></th>" - html = html + "<th><font color = \"white\">status</font></th>" - html = html + "<th><font color = \"white\">pass</font></th>" - html = html + "<th><font color = \"white\">xpass</font></th>" - html = html + "<th><font color = \"white\">fail</font></th>" - html = html + "<th><font color = \"white\">xfail</font></th>" - html = html + "<th><font color = \"white\">skip</font></th>" - html = html + "</tr>" - - #Add content to the table - bgcolor = 0 - for test_suite in summary_data: - if test_suite == "summary": - continue - if bgcolor == 0: - html = html + "<tr bgcolor = \"CCCBE4\">" - bgcolor = 1 - else: - html = html + "<tr bgcolor = \"E8E7F2\">" - bgcolor = 0 - html = html + "<th>" + test_suite + "</th>" - html = html + "<th>" + summary_data[test_suite]["test_status"] + "</th>" - html = html + "<th>" + str(summary_data[test_suite]["passed"]) + "</th>" - html = html + "<th>" + str(summary_data[test_suite]["xpassed"]) + "</th>" - html = html + "<th>" + str(summary_data[test_suite]["failed"]) + "</th>" - html = html + "<th>" + str(summary_data[test_suite]["xfailed"]) + "</th>" - html = html + "<th>" + str(summary_data[test_suite]["skipped"]) + "</th>" - html = html + "</tr>" - - html = html + "</table>" - html = html + "<p></p>" - html = html + "<font>Detail log :</font>" - #TODO update the link address for agl-test-log-xxx.zip - html = html + "<a href=\"" + "address of agl-test-log-xxx.zip " - html = html + "\">" + zip_name + ".zip</a>" - html = html + "</body>" - html = html + "</html>" - - return html +def format_test_suite_table(summary_data): + 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 suite </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> status </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> total </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> pass </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> xpass </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> fail </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> xfail </font></th>\n" + html_data += "\t\t<th><font color = \"" + color + "\"> skip </font></th>\n" + html_data += "\t</tr>\n" + + #init all rows + bgcolor = conf.BGCOLOR_DARK + for key in summary_data: + if(key != "summary"): + html_data += "\t<tr bgcolor = \"" + bgcolor + "\">\n" + html_data += "\t\t<th>" + key + "</th>\n" + html_data += "\t\t<th>" + summary_data[key]["test_status"] + "</th>\n" + html_data += "\t\t<th>" + str(summary_data[key]["total"]) + "</th>\n" + html_data += "\t\t<th>" + str(summary_data[key]["passed"]) + "</th>\n" + html_data += "\t\t<th>" + str(summary_data[key]["xpassed"]) + "</th>\n" + html_data += "\t\t<th>" + str(summary_data[key]["failed"]) + "</th>\n" + html_data += "\t\t<th>" + str(summary_data[key]["xfailed"]) + "</th>\n" + html_data += "\t\t<th>" + str(summary_data[key]["skipped"]) + "</th>\n" + html_data += "\t</tr>\n" + + if (bgcolor == conf.BGCOLOR_DARK): + bgcolor = conf.BGCOLOR_LIGHT + else: + bgcolor = conf.BGCOLOR_DARK + summary_data["test_suite_table_html"] = html_data + return summary_data diff --git a/template/all_test_suites_tpl.html b/template/all_test_suites_tpl.html new file mode 100644 index 0000000..5b3e0aa --- /dev/null +++ b/template/all_test_suites_tpl.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html> + <head> + <title> Summary Report </title> + </head> + <body> + <h1> Summary Report </h1> + <li> Status : {{ data.summary.status }} </li> + <li> Total : {{ data.summary.summary_total }} Pass : {{ data.summary.summary_passed }} Fail : {{ data.summary.summary_failed }} Skip : {{ data.summary.summary_skipped }} </li> + <li> Details : </li> + <table border="1" cellspacing="2"> +{{ data.test_suite_table_html }} + </table> + <p></p> + <li>Detail log : {{ data.log_zip_name }}.zip</li> + </body> +</html> |