diff --git a/common/testexecresults.py b/common/testexecresults.py index 485847e..7cdda55 100644 --- a/common/testexecresults.py +++ b/common/testexecresults.py @@ -7,6 +7,10 @@ from .resultsview import RunCommandResultsView from .testresult import TestResults +import matplotlib.pyplot as plt +import numpy as np +import re + class TestExecResults(): def __init__(self, test_results): @@ -31,3 +35,33 @@ def get_ExecuteNotebookResult(self, notebook_path, test_results): 'N/A', None, test_results) return ExecuteNotebookResult('N/A', 'N/A', notebook_result, 'N/A') + + def plot_pie_chart(self, title=None, legend=False, size=5): + """ + Plot a pie chart representing the distribution of test cases between 'Passed' and 'Failed' categories. + + Parameters: + - title (str, optional): Title for the pie chart. + - legend (bool, optional): Whether to display a legend. + - size (int, optional): Size of the pie chart. + + This method accepts three parameters: + 1. title (str, optional): Title for the pie chart. + 2. legend (bool, optional): Set to True to display a legend; False to hide it. + 3. size (int, optional): Size of the pie chart (both width and height). + + Note: The pie chart is based on the test results in the 'self.test_results' attribute. + """ + pass_fail_count = self.test_results.get_counts() + total_testcases = sum(pass_fail_count) + + plt.figure(figsize=(size, size)) + plt.pie(np.array(pass_fail_count), labels=["Passed", "Failed"], + autopct=lambda p: '' if re.search(r'^0+$', string='{:.0f}'.format(p * total_testcases / 100)) else '{:.0f}'.format(p * total_testcases / 100), + shadow=True, colors=["#4CAF50", "red"]) + if legend: + plt.legend(title="Test Result", bbox_to_anchor=(1.1, 0.5), bbox_transform=plt.gcf().transFigure, + loc="lower right") + if title is not None: + plt.title(title, fontweight='bold') + plt.show() diff --git a/common/testresult.py b/common/testresult.py index e21767e..dde1fe0 100644 --- a/common/testresult.py +++ b/common/testresult.py @@ -14,6 +14,7 @@ def get_test_results(): return TestResults() + class TestResults(PickleSerializable): def __init__(self): self.results = [] @@ -27,7 +28,7 @@ def append(self, testresult): self.results.append(testresult) self.test_cases = self.test_cases + 1 - if (not testresult.passed): + if not testresult.passed: self.num_failures = self.num_failures + 1 total_execution_time = self.total_execution_time + testresult.execution_time @@ -64,11 +65,20 @@ def __eq__(self, other): def __item_in_list_equalto(self, expected_item): for item in self.results: - if (item == expected_item): + if item == expected_item: return True return False + def get_counts(self): + """ + This method returns the tuple having frequency of passed and failed test cases. + """ + passed = sum(1 for item in self.results if item.passed) + failed = len(self.results) - passed + return passed, failed + + class TestResult: def __init__(self, test_name, passed, execution_time, tags, exception=None, stack_trace=""): diff --git a/requirements.txt b/requirements.txt index e0db8e8..1feaba2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,6 @@ requests fire junit_xml py4j - +numpy +matplotlib +regex