def __compose_summary_from_cobertura_document( self, cobertura_document: Any, coverage_provider_name: str) -> CoverageTotals: """ Compose a CoverageTotals instance from the Cobetura based document. """ project_name = cobertura_document.find("./sources/source").text project_name = os.path.basename(project_name) coverage_totals = CoverageTotals(project_name=project_name, report_source=coverage_provider_name) measured_lines, covered_lines, measured_branches, covered_branches = 0, 0, 0, 0 for next_package in cobertura_document.findall("./packages/package"): for next_class in next_package.findall("./classes/class"): for next_line in next_class.findall("./lines/line"): ( covered_line_delta, line_branch_coverage, line_branch_measured, ) = self.__process_line_node(next_line) measured_lines += 1 covered_lines += covered_line_delta covered_branches += line_branch_coverage measured_branches += line_branch_measured coverage_totals.line_level = CoverageMeasurement( total_covered=covered_lines, total_measured=measured_lines) coverage_totals.branch_level = CoverageMeasurement( total_covered=covered_branches, total_measured=measured_branches) return coverage_totals
def test_made_up_profile(): """ Test to make sure the model can test a fictional profile that does not have any line or branch measurements, say a coverage of assembly instructions. """ # Arrange coverage_profile = CoverageTotals(project_name="?", report_source="asmunit") coverage_profile.instruction_level = CoverageMeasurement(total_covered=25, total_measured=30) g_instructions = {"totalCovered": 25, "totalMeasured": 30} expected_dictionary = { "projectName": "?", "reportSource": "asmunit", "instructionLevel": g_instructions, } # Act coverage_profile_as_dictionary = coverage_profile.to_dict() back_from_dictionary = CoverageTotals.from_dict( coverage_profile_as_dictionary) # Assert assert coverage_profile_as_dictionary == expected_dictionary assert back_from_dictionary == coverage_profile
def test_coverage_totals_not_equal(): """ Test to make sure the coverage totals is not equal to another total of almost the same amount. """ # Arrange first_line_coverage_measured = 20 second_line_coverage_measured = 21 first_total = CoverageTotals(project_name="?", report_source="pytest") first_total.branch_level = CoverageMeasurement(total_covered=5, total_measured=10) first_total.line_level = CoverageMeasurement( total_covered=15, total_measured=first_line_coverage_measured ) second_total = CoverageTotals(project_name="?", report_source="pytest") second_total.branch_level = CoverageMeasurement(total_covered=5, total_measured=10) second_total.line_level = CoverageMeasurement( total_covered=15, total_measured=second_line_coverage_measured ) # Act actual_result = first_total == second_total # Assert assert not actual_result
def test_coverage_totals_not_equal_different_object(): """ Test to make sure the coverage totals is not equal to another object that is not a total object. """ # Arrange first_total = CoverageTotals(project_name="?", report_source="pytest") first_total.branch_level = CoverageMeasurement(total_covered=5, total_measured=10) first_total.line_level = CoverageMeasurement(total_covered=15, total_measured=20) # Act actual_result = first_total == "second_total" # Assert assert not actual_result
def __load_coverage_results_summary_file( cls, test_results_to_load: str) -> Optional[CoverageTotals]: """ Attempt to load a previously published test summary. """ test_totals = None if os.path.exists(test_results_to_load) and os.path.isfile( test_results_to_load): try: with open( os.path.abspath(test_results_to_load), "r", encoding=ProjectSummarizerPlugin.DEFAULT_FILE_ENCODING, ) as infile: results_dictionary = json.load(infile) except json.decoder.JSONDecodeError as ex: print( f"Previous coverage summary file '{test_results_to_load}' is not a valid JSON file ({ex})." ) sys.exit(1) except IOError as ex: print( f"Previous coverage summary file '{test_results_to_load}' was not loaded ({ex})." ) sys.exit(1) test_totals = CoverageTotals.from_dict(results_dictionary) return test_totals
def test_junit_jacoco_profile(): """ Test to make sure the model handles the jacoco profile used by junit. """ # Arrange coverage_profile = CoverageTotals(project_name="?", report_source="junit") coverage_profile.instruction_level = CoverageMeasurement(total_covered=25, total_measured=30) coverage_profile.line_level = CoverageMeasurement(total_covered=15, total_measured=20) coverage_profile.branch_level = CoverageMeasurement(total_covered=5, total_measured=10) coverage_profile.complexity_level = CoverageMeasurement(total_covered=6, total_measured=11) coverage_profile.method_level = CoverageMeasurement(total_covered=3, total_measured=4) coverage_profile.class_level = CoverageMeasurement(total_covered=1, total_measured=1) g_instructions = {"totalCovered": 25, "totalMeasured": 30} g_line = {"totalCovered": 15, "totalMeasured": 20} g_branch = {"totalCovered": 5, "totalMeasured": 10} g_complexity = {"totalCovered": 6, "totalMeasured": 11} g_method = {"totalCovered": 3, "totalMeasured": 4} g_class = {"totalCovered": 1, "totalMeasured": 1} expected_dictionary = { "projectName": "?", "reportSource": "junit", "instructionLevel": g_instructions, "lineLevel": g_line, "branchLevel": g_branch, "complexityLevel": g_complexity, "methodLevel": g_method, "classLevel": g_class, } # Act coverage_profile_as_dictionary = coverage_profile.to_dict() back_from_dictionary = CoverageTotals.from_dict( coverage_profile_as_dictionary) # Assert assert coverage_profile_as_dictionary == expected_dictionary assert back_from_dictionary == coverage_profile
def test_pytest_cobertura_profile(): """ Test to make sure the model handles the cobertura profile used by pytest. """ # Arrange coverage_profile = CoverageTotals(project_name="?", report_source="pytest") coverage_profile.branch_level = CoverageMeasurement(total_covered=5, total_measured=10) coverage_profile.line_level = CoverageMeasurement(total_covered=15, total_measured=20) g_branch = {"totalCovered": 5, "totalMeasured": 10} g_line = {"totalCovered": 15, "totalMeasured": 20} expected_dictionary = { "projectName": "?", "reportSource": "pytest", "branchLevel": g_branch, "lineLevel": g_line, } # Act coverage_profile_as_dictionary = coverage_profile.to_dict() back_from_dictionary = CoverageTotals.from_dict( coverage_profile_as_dictionary) # Assert assert coverage_profile_as_dictionary == expected_dictionary assert back_from_dictionary == coverage_profile
def __report_coverage_files( self, new_stats: CoverageTotals, loaded_stats: Optional[CoverageTotals], only_report_changes: bool, column_width: int, ) -> None: """ Create a report on coverage. """ if not loaded_stats: loaded_stats = CoverageTotals() report_rows = self.__calculate_coverage_rows(new_stats, loaded_stats) test_report_rows = self.__format_coverage_rows(report_rows, only_report_changes) print("\nTest Coverage Summary\n---------------------\n") if not test_report_rows: print( "Test coverage has not changed since last published test coverage." ) else: hdrs = ["Type", "Covered", "Measured", "Percentage"] table = columnar( test_report_rows, headers=hdrs, no_borders=True, justify=["l", "r", "r", "r"], terminal_width=column_width if column_width != -1 else None, ) split_rows = table.split("\n") new_rows = [next_row.rstrip() for next_row in split_rows] print("\n".join(new_rows)) print()