Loading parse_test_results.py +7 −2 Original line number Original line Diff line number Diff line Loading @@ -19,16 +19,15 @@ class TestOutputParser: """ """ def __init__(self, input_file, output_file): def __init__(self, input_file, output_file): self.test_entries = [] self.test_entries = [] self.load_file(input_file) self.api = "" self.api = "" self.output_file = output_file self.output_file = output_file self.robot_file_contents = "" self.robot_file_contents = "" self.load_file(input_file) def load_file(self, fname): def load_file(self, fname): """ """ Load xml file Load xml file """ """ with open(fname, "r", encoding="utf8") as robot_file: with open(fname, "r", encoding="utf8") as robot_file: self.robot_file_contents = robot_file.read() self.robot_file_contents = robot_file.read() Loading @@ -40,6 +39,7 @@ class TestOutputParser: """ """ Run parser, extracting all info to create the test entries Run parser, extracting all info to create the test entries """ """ soup = BeautifulSoup(self.robot_file_contents, "lxml") soup = BeautifulSoup(self.robot_file_contents, "lxml") # Suite information # Suite information Loading @@ -60,6 +60,11 @@ class TestOutputParser: excel_writer = ExcelWriter(self.output_file) excel_writer = ExcelWriter(self.output_file) for entry in self.test_entries: for entry in self.test_entries: excel_writer.write_test_entry(entry) excel_writer.write_test_entry(entry) # Write chart excel_writer.write_pie_chart_data() # And finally, save the work book excel_writer.save() excel_writer.save() def create_test_entry(self, xml_obj): def create_test_entry(self, xml_obj): Loading write_excel.py +63 −3 Original line number Original line Diff line number Diff line Loading @@ -6,6 +6,7 @@ from openpyxl import Workbook, load_workbook from openpyxl.utils import get_column_letter from openpyxl.utils import get_column_letter from openpyxl.styles import Font from openpyxl.styles import Font from openpyxl.styles.fills import PatternFill from openpyxl.styles.fills import PatternFill from openpyxl.chart import PieChart, Reference class ExcelWriter: class ExcelWriter: """ """ Loading @@ -15,9 +16,9 @@ class ExcelWriter: PASS_COL = "00FF00" PASS_COL = "00FF00" FAIL_COL = "FF0000" FAIL_COL = "FF0000" def __init__(self, output_file): def __init__(self, output_file): self.output_file = output_file self.work_book = self.get_workbook() self.work_book = self.get_workbook() self.work_sheet = self.work_book.active self.work_sheet = self.work_book.active self.output_file = output_file def get_entry_with_id(self, test_id): def get_entry_with_id(self, test_id): """ """ Loading @@ -42,13 +43,13 @@ class ExcelWriter: for cell in self.work_sheet["A"]: for cell in self.work_sheet["A"]: if cell.value is None: if cell.value is None: return cell.row return cell.row return 1 return cell.row + 1 def write_test_entry(self, test_entry): def write_test_entry(self, test_entry): """ """ Write a test entry to the work_sheet Write a test entry to the work_sheet """ """ existing_entry_row = self.get_entry_with_id(test_entry.id) existing_entry_row = self.get_entry_with_id(test_entry.test_id) last_row = self.get_last_row() last_row = self.get_last_row() # Use the above two values to pick a row # Use the above two values to pick a row entry_row = existing_entry_row if existing_entry_row != -1 else last_row entry_row = existing_entry_row if existing_entry_row != -1 else last_row Loading Loading @@ -79,6 +80,65 @@ class ExcelWriter: init_workbook(work_book) init_workbook(work_book) return work_book return work_book def get_chart_work_sheet(self): """ Returns work sheet where charts will be drawn to. Creates if does not exist """ try: chart_work_sheet = self.work_book["charts"] return chart_work_sheet except KeyError: return self.work_book.create_sheet("charts") def write_pie_chart_data(self): """ Writes pie chart data (the charts sheet) required to construct the pie chart later """ chart_work_sheet = self.get_chart_work_sheet() # Format data from main work sheet and insert into charts sheet # Titles chart_work_sheet["A2"] = "PASS" chart_work_sheet["A3"] = "FAIL" #Data data = self.get_pass_fail_counts() chart_work_sheet.cell(row=2, column=2).value = data[0] chart_work_sheet.cell(row=3, column=2).value = data[1] # Get data from main work sheet data = Reference(chart_work_sheet, min_col=2, max_col=2, min_row=2, max_row=3) labels = Reference(chart_work_sheet, min_col=1, max_col=1, min_row=2, max_row=3) # Construct pie chart pie = PieChart() pie.add_data(data)#, titles_from_data=True) pie.set_categories(labels) pie.title = "PASS/FAIL Distribution" chart_work_sheet.add_chart(pie, "E1") def get_pass_fail_counts(self): """ Return PASS/FAIL stats as tuple (# of PASS, # of FAIL) """ # Get data from main work sheet last_row = self.get_last_row() pass_count = 0 fail_count = 0 for row in self.work_sheet.iter_rows(min_col=3, max_col=3, min_row=2, max_row=last_row - 1): for cell in row: if cell.value == "PASS": pass_count += 1 elif cell.value == "FAIL": fail_count += 1 else: print("ERROR: unknown value {}".format(cell.value)) return (pass_count, fail_count) def save(self): def save(self): """ """ Save workbook to disk. Save workbook to disk. Loading Loading
parse_test_results.py +7 −2 Original line number Original line Diff line number Diff line Loading @@ -19,16 +19,15 @@ class TestOutputParser: """ """ def __init__(self, input_file, output_file): def __init__(self, input_file, output_file): self.test_entries = [] self.test_entries = [] self.load_file(input_file) self.api = "" self.api = "" self.output_file = output_file self.output_file = output_file self.robot_file_contents = "" self.robot_file_contents = "" self.load_file(input_file) def load_file(self, fname): def load_file(self, fname): """ """ Load xml file Load xml file """ """ with open(fname, "r", encoding="utf8") as robot_file: with open(fname, "r", encoding="utf8") as robot_file: self.robot_file_contents = robot_file.read() self.robot_file_contents = robot_file.read() Loading @@ -40,6 +39,7 @@ class TestOutputParser: """ """ Run parser, extracting all info to create the test entries Run parser, extracting all info to create the test entries """ """ soup = BeautifulSoup(self.robot_file_contents, "lxml") soup = BeautifulSoup(self.robot_file_contents, "lxml") # Suite information # Suite information Loading @@ -60,6 +60,11 @@ class TestOutputParser: excel_writer = ExcelWriter(self.output_file) excel_writer = ExcelWriter(self.output_file) for entry in self.test_entries: for entry in self.test_entries: excel_writer.write_test_entry(entry) excel_writer.write_test_entry(entry) # Write chart excel_writer.write_pie_chart_data() # And finally, save the work book excel_writer.save() excel_writer.save() def create_test_entry(self, xml_obj): def create_test_entry(self, xml_obj): Loading
write_excel.py +63 −3 Original line number Original line Diff line number Diff line Loading @@ -6,6 +6,7 @@ from openpyxl import Workbook, load_workbook from openpyxl.utils import get_column_letter from openpyxl.utils import get_column_letter from openpyxl.styles import Font from openpyxl.styles import Font from openpyxl.styles.fills import PatternFill from openpyxl.styles.fills import PatternFill from openpyxl.chart import PieChart, Reference class ExcelWriter: class ExcelWriter: """ """ Loading @@ -15,9 +16,9 @@ class ExcelWriter: PASS_COL = "00FF00" PASS_COL = "00FF00" FAIL_COL = "FF0000" FAIL_COL = "FF0000" def __init__(self, output_file): def __init__(self, output_file): self.output_file = output_file self.work_book = self.get_workbook() self.work_book = self.get_workbook() self.work_sheet = self.work_book.active self.work_sheet = self.work_book.active self.output_file = output_file def get_entry_with_id(self, test_id): def get_entry_with_id(self, test_id): """ """ Loading @@ -42,13 +43,13 @@ class ExcelWriter: for cell in self.work_sheet["A"]: for cell in self.work_sheet["A"]: if cell.value is None: if cell.value is None: return cell.row return cell.row return 1 return cell.row + 1 def write_test_entry(self, test_entry): def write_test_entry(self, test_entry): """ """ Write a test entry to the work_sheet Write a test entry to the work_sheet """ """ existing_entry_row = self.get_entry_with_id(test_entry.id) existing_entry_row = self.get_entry_with_id(test_entry.test_id) last_row = self.get_last_row() last_row = self.get_last_row() # Use the above two values to pick a row # Use the above two values to pick a row entry_row = existing_entry_row if existing_entry_row != -1 else last_row entry_row = existing_entry_row if existing_entry_row != -1 else last_row Loading Loading @@ -79,6 +80,65 @@ class ExcelWriter: init_workbook(work_book) init_workbook(work_book) return work_book return work_book def get_chart_work_sheet(self): """ Returns work sheet where charts will be drawn to. Creates if does not exist """ try: chart_work_sheet = self.work_book["charts"] return chart_work_sheet except KeyError: return self.work_book.create_sheet("charts") def write_pie_chart_data(self): """ Writes pie chart data (the charts sheet) required to construct the pie chart later """ chart_work_sheet = self.get_chart_work_sheet() # Format data from main work sheet and insert into charts sheet # Titles chart_work_sheet["A2"] = "PASS" chart_work_sheet["A3"] = "FAIL" #Data data = self.get_pass_fail_counts() chart_work_sheet.cell(row=2, column=2).value = data[0] chart_work_sheet.cell(row=3, column=2).value = data[1] # Get data from main work sheet data = Reference(chart_work_sheet, min_col=2, max_col=2, min_row=2, max_row=3) labels = Reference(chart_work_sheet, min_col=1, max_col=1, min_row=2, max_row=3) # Construct pie chart pie = PieChart() pie.add_data(data)#, titles_from_data=True) pie.set_categories(labels) pie.title = "PASS/FAIL Distribution" chart_work_sheet.add_chart(pie, "E1") def get_pass_fail_counts(self): """ Return PASS/FAIL stats as tuple (# of PASS, # of FAIL) """ # Get data from main work sheet last_row = self.get_last_row() pass_count = 0 fail_count = 0 for row in self.work_sheet.iter_rows(min_col=3, max_col=3, min_row=2, max_row=last_row - 1): for cell in row: if cell.value == "PASS": pass_count += 1 elif cell.value == "FAIL": fail_count += 1 else: print("ERROR: unknown value {}".format(cell.value)) return (pass_count, fail_count) def save(self): def save(self): """ """ Save workbook to disk. Save workbook to disk. Loading