Spaces:
Runtime error
Runtime error
File size: 6,378 Bytes
5fc6e5d |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
from datetime import datetime
import platform
import sys
from typing import Optional
from loguru import logger
import pandas as pd
from turing.config import REPORTS_DIR
class TestReportGenerator:
"""
Handles the generation of structured Markdown reports specifically for test execution results.
"""
def __init__(self, context_name: str, report_category: str):
self.context_name = context_name
self.report_category = report_category
self.timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
self.content = []
self.output_dir = REPORTS_DIR / self.report_category
def add_header(self, text: str, level: int = 1):
self.content.append(f"\n{'#' * level} {text}\n")
def add_divider(self, style: str = "thin"):
"""Add a visual divider line."""
dividers = {
"thin": "---",
"thick": "___",
"section": "\n---\n",
}
self.content.append(f"\n{dividers.get(style, dividers['thin'])}\n")
def add_code_block(self, content: str, language: str = ""):
"""Add a code block."""
self.content.append(f"\n```{language}\n{content}\n```\n")
def add_alert_box(self, message: str, box_type: str = "info"):
"""Add a styled alert box using blockquotes."""
box_headers = {
"info": "INFO",
"success": "SUCCESS",
"warning": "WARNING",
"error": "ERROR",
}
header = box_headers.get(box_type, "INFO")
self.content.append(f"\n> **{header}**: {message}\n")
def add_progress_bar(self, passed: int, total: int, width: int = 50):
"""Add an ASCII progress bar."""
if total == 0:
percentage = 0
filled = 0
else:
percentage = (passed / total * 100)
filled = int(width * passed / total)
empty = width - filled
bar = "█" * filled + "░" * empty
self.add_code_block(f"Progress: [{bar}] {percentage:.1f}%\nPassed: {passed}/{total} tests", "")
def add_summary_box(self, total: int, passed: int, failed: int, skipped: int = 0):
"""Add a visually enhanced summary box."""
success_rate = (passed / total * 100) if total > 0 else 0
# Determine status
if success_rate == 100:
status = "ALL TESTS PASSED"
elif success_rate >= 80:
status = "MOSTLY PASSED"
elif success_rate >= 50:
status = "PARTIAL SUCCESS"
else:
status = "NEEDS ATTENTION"
self.add_header("Executive Summary", level=2)
self.add_text(f"**Overall Status:** {status}")
self.add_text(f"**Success Rate:** {success_rate:.1f}%")
# Summary table
summary_data = [
["Total Tests", str(total)],
["Passed", str(passed)],
["Failed", str(failed)],
]
if skipped > 0:
summary_data.append(["Skipped", str(skipped)])
summary_data.append(["Success Rate", f"{success_rate:.1f}%"])
df = pd.DataFrame(summary_data, columns=["Metric", "Count"])
self.add_dataframe(df, title=None, align=("left", "right"))
# Progress bar
self.add_text("**Visual Progress:**")
self.add_progress_bar(passed, total)
def add_environment_metadata(self):
"""Add enhanced environment metadata."""
self.add_header("Environment Information", level=2)
metadata = [
["Timestamp", datetime.now().strftime("%Y-%m-%d %H:%M:%S")],
["Context", self.context_name.upper()],
["Python Version", sys.version.split()[0]],
["Platform", platform.platform()],
["Architecture", platform.machine()],
]
df = pd.DataFrame(metadata, columns=["Parameter", "Value"])
self.add_dataframe(df, title=None, align=("left", "left"))
def add_text(self, text: str):
self.content.append(f"\n{text}\n")
def add_category_stats(self, df: pd.DataFrame, category: str):
"""Add statistics for a test category."""
total = len(df)
passed = len(df[df['Result'] == "PASS"])
failed = len(df[df['Result'] == "FAIL"])
skipped = len(df[df['Result'] == "SKIP"])
stats = [
["Total", str(total)],
["Passed", f"{passed} ({passed/total*100:.1f}%)" if total > 0 else "0"],
["Failed", f"{failed} ({failed/total*100:.1f}%)" if total > 0 else "0"],
]
if skipped > 0:
stats.append(["Skipped", f"{skipped} ({skipped/total*100:.1f}%)"])
stats_df = pd.DataFrame(stats, columns=["Status", "Count"])
self.add_dataframe(stats_df, title="Statistics", align=("left", "right"))
def add_dataframe(self, df: pd.DataFrame, title: Optional[str] = None, align: tuple = None):
"""Add a formatted dataframe table."""
if title:
self.add_header(title, level=3)
if df.empty:
self.content.append("\n_No data available._\n")
return
try:
if not align:
align = tuple(["left"] * len(df.columns))
table_md = df.to_markdown(index=False, tablefmt="pipe", colalign=align)
self.content.append(f"\n{table_md}\n")
except Exception as e:
logger.warning(f"Tabulate error: {e}. Using simple text.")
self.content.append(f"\n```text\n{df.to_string(index=False)}\n```\n")
def save(self, filename: str = "test_report.md") -> str:
"""Save the report to a file."""
try:
self.output_dir.mkdir(parents=True, exist_ok=True)
file_path = self.output_dir / filename
# Add footer
self.add_divider("section")
self.add_text(f"*Report generated on {datetime.now().strftime('%Y-%m-%d at %H:%M:%S')}*")
self.add_text("*Powered by Turing Test Suite*")
with open(file_path, "w", encoding="utf-8") as f:
f.write("\n".join(self.content))
logger.info(f"Test report saved: {file_path}")
return str(file_path)
except Exception as e:
logger.error(f"Save failed: {e}")
raise
|