Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Paebbels authored Feb 24, 2024
2 parents 99b9a6b + eed288d commit 749fc70
Show file tree
Hide file tree
Showing 48 changed files with 21,962 additions and 2,736 deletions.
445 changes: 160 additions & 285 deletions pyEDAA/Reports/Unittesting/JUnit.py

Large diffs are not rendered by default.

166 changes: 125 additions & 41 deletions pyEDAA/Reports/Unittesting/OSVVM.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,77 +29,161 @@
# ==================================================================================================================== #
#
"""Reader for OSVVM test report summary files in YAML format."""
from datetime import timedelta, datetime
from pathlib import Path
from typing import Dict
from time import perf_counter_ns
from typing import Optional as Nullable

from ruamel.yaml import YAML
from pyTooling.Decorators import export
from pyTooling.Decorators import export, notimplemented

from pyEDAA.Reports.Unittesting import Testsuite as Abstract_Testsuite, Testcase as Abstract_Testcase, Status
from pyEDAA.Reports.Unittesting import UnittestException, Document, TestcaseStatus
from pyEDAA.Reports.Unittesting import TestsuiteSummary as ut_TestsuiteSummary, Testsuite as ut_Testsuite
from pyEDAA.Reports.Unittesting import Testcase as ut_Testcase


@export
class Testsuite(Abstract_Testsuite):
class OsvvmException:
pass


@export
class Testcase(Abstract_Testcase):
class UnittestException(UnittestException, OsvvmException):
pass


@export
class Document:
_yamlDocument: YAML
_testsuites: Dict[str, Testsuite]
class TestsuiteSummary(ut_TestsuiteSummary):
pass

def __init__(self, yamlReportFile: Path) -> None:
yamlReader = YAML()
self._yamlDocument = yamlReader.load(yamlReportFile)
yamlBuild = self._yamlDocument["BuildInfo"]

self._testsuites = {}
@export
class Testsuite(ut_Testsuite):
pass

self.translateDocument()

def translateDocument(self) -> None:
for yamlTestsuite in self._yamlDocument['TestSuites']:
name = yamlTestsuite["Name"]
self._testsuites[name] = self.translateTestsuite(yamlTestsuite, name)
@export
class Testcase(ut_Testcase):
pass

def translateTestsuite(self, yamlTestsuite, name) -> Testsuite:
testsuite = Testsuite(name)

for yamlTestcase in yamlTestsuite['TestCases']:
testcaseName = yamlTestcase["Name"]
testsuite._testcases[testcaseName] = self.translateTestcase(yamlTestcase, testcaseName)
@export
class OsvvmYamlDocument(TestsuiteSummary, Document):
_yamlDocument: Nullable[YAML]

return testsuite
def __init__(self, yamlReportFile: Path, parse: bool = False) -> None:
super().__init__("Unread JUnit XML file")
Document.__init__(self, yamlReportFile)

def translateTestcase(self, yamlTestcase, name) -> Testcase:
yamlStatus = yamlTestcase["Status"].lower()
self._yamlDocument = None

assertionCount = 0
warningCount = 0
errorCount = 0
fatalCount = 0
if parse:
self.Read()
self.Parse()

if yamlStatus == "passed":
status = Status.Passed
def Read(self) -> None:
if not self._path.exists():
raise UnittestException(f"OSVVM YAML file '{self._path}' does not exist.") \
from FileNotFoundError(f"File '{self._path}' not found.")

yamlResults = yamlTestcase["Results"]
assertionCount = yamlResults["AffirmCount"]
startAnalysis = perf_counter_ns()
try:
yamlReader = YAML()
self._yamlDocument = yamlReader.load(self._path)
except Exception as ex:
raise UnittestException(f"Couldn't open '{self._path}'.") from ex

elif yamlStatus == "skipped":
status = Status.Skipped
endAnalysis = perf_counter_ns()
self._analysisDuration = (endAnalysis - startAnalysis) / 1e9

elif yamlStatus == "failed":
status = Status.Failed
@notimplemented
def Write(self, path: Nullable[Path] = None, overwrite: bool = False) -> None:
if path is None:
path = self._path

else:
print(f"ERROR: Unknown testcase status '{yamlStatus}'.")
if not overwrite and path.exists():
raise UnittestException(f"OSVVM YAML file '{path}' can not be written.") \
from FileExistsError(f"File '{path}' already exists.")

# if regenerate:
# self.Generate(overwrite=True)

if self._yamlDocument is None:
ex = UnittestException(f"Internal YAML document tree is empty and needs to be generated before write is possible.")
ex.add_note(f"Call 'OsvvmYamlDocument.Generate()' or 'OsvvmYamlDocument.Write(..., regenerate=True)'.")
raise ex

# with path.open("w") as file:
# self._yamlDocument.writexml(file, addindent="\t", encoding="utf-8", newl="\n")

return Testcase(name, assertionCount, warningCount, errorCount, fatalCount)
def Parse(self) -> None:
if self._yamlDocument is None:
ex = UnittestException(f"OSVVM YAML file '{self._path}' needs to be read and analyzed by a YAML parser.")
ex.add_note(f"Call 'OsvvmYamlDocument.Read()' or create document using 'OsvvmYamlDocument(path, parse=True)'.")
raise ex

startConversion = perf_counter_ns()
self._startTime = datetime.fromisoformat(self._yamlDocument["Date"])
# yamlBuild = self._yamlDocument["BuildInfo"]

for yamlTestsuite in self._yamlDocument['TestSuites']:
self._ParseTestsuite(self, yamlTestsuite)

self.Aggregate()
endConversation = perf_counter_ns()
self._modelConversion = (endConversation - startConversion) / 1e9

def _ParseTestsuite(self, parentTestsuite: Testsuite, yamlTestsuite) -> None:
testsuiteName = yamlTestsuite["Name"]
totalDuration = timedelta(seconds=float(yamlTestsuite["ElapsedTime"]))

testsuite = Testsuite(
testsuiteName,
totalDuration=totalDuration,
parent=parentTestsuite
)

for yamlTestcase in yamlTestsuite['TestCases']:
self._ParseTestcase(testsuite, yamlTestcase)

def _ParseTestcase(self, parentTestsuite: Testsuite, yamlTestcase) -> None:
testcaseName = yamlTestcase["Name"]
totalDuration = timedelta(seconds=float(yamlTestcase["ElapsedTime"]))
yamlStatus = yamlTestcase["Status"].lower()
yamlResults = yamlTestcase["Results"]
assertionCount = int(yamlResults["AffirmCount"])
passedAssertionCount = int(yamlResults["PassedCount"])
warningCount = int(yamlResults["AlertCount"]["Warning"])
errorCount = int(yamlResults["AlertCount"]["Error"])
fatalCount = int(yamlResults["AlertCount"]["Failure"])

if yamlStatus == "passed":
status = TestcaseStatus.Passed
elif yamlStatus == "skipped":
status = TestcaseStatus.Skipped
elif yamlStatus == "failed":
status = TestcaseStatus.Failed
else:
status = TestcaseStatus.Unknown

if warningCount > 0:
status |= TestcaseStatus.Warned
if errorCount > 0:
status |= TestcaseStatus.Errored
if fatalCount > 0:
status |= TestcaseStatus.Aborted

testcase = Testcase(
testcaseName,
totalDuration=totalDuration,
assertionCount=assertionCount,
passedAssertionCount=passedAssertionCount,
warningCount=warningCount,
status=status,
errorCount=errorCount,
fatalCount=fatalCount,
parent=parentTestsuite
)

def __contains__(self, key: str) -> bool:
return key in self._testsuites
Expand Down
Loading

0 comments on commit 749fc70

Please sign in to comment.