1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
import json
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import List
from jinja2 import Environment, FileSystemLoader, select_autoescape
from onapsdk.configuration import settings
from onapsdk.exceptions import SettingsError
from onaptests.utils.resources import get_resource_location
class ReportStepStatus(Enum):
"""Enum which stores steps execution statuses."""
PASS = "PASS"
FAIL = "FAIL"
NOT_EXECUTED = "NOT EXECUTED"
@dataclass
class Report:
"""Step execution report."""
step_description: str
step_execution_status: ReportStepStatus
step_execution_duration: float
step_component: str
step_error_reason: List[str]
class ReportsCollection:
"""Collection to store steps execution statuses."""
def __init__(self, components: list) -> None:
"""Initialize collection."""
self._collection: list = []
self._components = components
def put(self, item: Report) -> None:
"""Put execution status dictionary.
Args:
item (Report): Step report
"""
self._collection.insert(0, item)
@property
def report(self) -> List[Report]:
"""Get report.
Build a dictionary with execution statuses.
Returns:
List[str, str]: Steps name with status dictionary
"""
return self._collection
@property
def failed_steps_num(self) -> int:
"""Number of failed steps in report.
Returns:
int: How many steps failed
"""
return sum((1 for step_report in self.report if
step_report.step_execution_status == ReportStepStatus.FAIL))
def generate_report(self) -> None:
"""Generate report files after execution of the test."""
usecase = settings.SERVICE_NAME
try:
details = settings.SERVICE_DETAILS
except (KeyError, AttributeError, SettingsError):
details = ""
components = ""
for component in self._components:
components = f"{component}, {components}"
components = components.rstrip(", ")
jinja_env = Environment(
autoescape=select_autoescape(['html']),
loader=FileSystemLoader(get_resource_location('templates/reporting')))
jinja_env.get_template('reporting.html.j2').stream(
report=self,
usecase=usecase,
details=details,
components=components,
log_path="./pythonsdk.debug.log").dump(
str(Path(settings.REPORTING_FILE_DIRECTORY).joinpath(
settings.HTML_REPORTING_FILE_NAME)))
report_dict = {
'usecase': usecase,
'details': details,
'components': components,
'steps': [
{
'description': step_report.step_description,
'status': step_report.step_execution_status.value,
'duration': step_report.step_execution_duration,
'component': step_report.step_component,
'reason': step_report.step_error_reason
}
for step_report in reversed(self.report)
]
}
with (Path(settings.REPORTING_FILE_DIRECTORY).joinpath(
settings.JSON_REPORTING_FILE_NAME)).open('w', encoding="utf-8") as file:
json.dump(report_dict, file, indent=4)
|