File size: 6,945 Bytes
aef804e | 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | #!/usr/bin/env python3
"""
Mutation Test Report Generator
Generates comprehensive mutation testing reports including:
- Mutation scores by module
- Surviving mutants analysis
- Test coverage gaps
- Recommendations for improvement
Usage:
python generate_mutation_report.py --target priority_p0_financial
python generate_mutation_report.py --all
"""
import argparse
import json
import subprocess
import sys
from pathlib import Path
from datetime import datetime
import configparser
def load_targets_config():
"""Load mutation testing targets configuration."""
config_path = Path(__file__).parent.parent / "targets" / "TARGETS.ini"
config = configparser.ConfigParser()
config.read(config_path)
return config
def get_mutmut_results():
"""Get mutation testing results from mutmut."""
try:
result = subprocess.run(
["mutmut", "results"],
capture_output=True,
text=True,
timeout=30
)
return result.stdout
except Exception as e:
print(f"Error getting mutmut results: {e}")
return None
def parse_mutmut_results(output):
"""Parse mutmut results into structured data."""
if not output:
return None
results = {
'score': 0.0,
'killed': 0,
'survived': 0,
'total': 0,
'modules': {}
}
lines = output.split('\n')
for line in lines:
if 'Mutation score:' in line:
import re
match = re.search(r'(\d+\.\d+)%', line)
if match:
results['score'] = float(match.group(1))
return results
def generate_html_report(results, output_path):
"""Generate HTML mutation testing report."""
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>Mutation Testing Report - Atom Platform</title>
<style>
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 40px;
background: #f5f5f5;
}}
.container {{
max-width: 1200px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}}
h1 {{
color: #333;
border-bottom: 2px solid #007bff;
padding-bottom: 10px;
}}
h2 {{
color: #555;
margin-top: 30px;
}}
.score {{
font-size: 48px;
font-weight: bold;
text-align: center;
margin: 30px 0;
}}
.score.good {{
color: #28a745;
}}
.score.warning {{
color: #ffc107;
}}
.score.danger {{
color: #dc3545;
}}
table {{
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}}
th, td {{
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}}
th {{
background: #f8f9fa;
font-weight: 600;
}}
.status-pass {{
color: #28a745;
font-weight: bold;
}}
.status-fail {{
color: #dc3545;
font-weight: bold;
}}
.recommendation {{
background: #f8f9fa;
padding: 15px;
border-left: 4px solid #007bff;
margin: 20px 0;
}}
</style>
</head>
<body>
<div class="container">
<h1>🧬 Mutation Testing Report</h1>
<p>Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
<div class="score {'good' if results.get('score', 0) >= 90 else 'warning' if results.get('score', 0) >= 80 else 'danger'}">
{results.get('score', 0):.2f}%
</div>
<h2>Results Summary</h2>
<table>
<tr>
<th>Metric</th>
<th>Value</th>
</tr>
<tr>
<td>Mutation Score</td>
<td>{results.get('score', 0):.2f}%</td>
</tr>
<tr>
<td>Killed Mutants</td>
<td>{results.get('killed', 0)}</td>
</tr>
<tr>
<td>Surviving Mutants</td>
<td>{results.get('survived', 0)}</td>
</tr>
<tr>
<td>Total Mutants</td>
<td>{results.get('total', 0)}</td>
</tr>
</table>
<h2>Quality Gates</h2>
<table>
<tr>
<th>Priority</th>
<th>Target</th>
<th>Status</th>
</tr>
<tr>
<td>P0: Financial & Security</td>
<td>>95%</td>
<td class="{'status-pass' if results.get('score', 0) >= 95 else 'status-fail'}">
{'✅ PASS' if results.get('score', 0) >= 95 else '❌ FAIL'}
</td>
</tr>
<tr>
<td>P1: Core Business Logic</td>
<td>>90%</td>
<td class="{'status-pass' if results.get('score', 0) >= 90 else 'status-fail'}">
{'✅ PASS' if results.get('score', 0) >= 90 else '❌ FAIL'}
</td>
</tr>
<tr>
<td>P2: API & Tools</td>
<td>>85%</td>
<td class="{'status-pass' if results.get('score', 0) >= 85 else 'status-fail'}">
{'✅ PASS' if results.get('score', 0) >= 85 else '❌ FAIL'}
</td>
</tr>
</table>
<div class="recommendation">
<h3>Recommendations</h3>
<ul>
<li>Add property-based tests to kill surviving mutants</li>
<li>Focus on testing edge cases and boundary conditions</li>
<li>Increase Hypothesis max_examples for better coverage</li>
<li>Review test assertions for completeness</li>
</ul>
</div>
</div>
</body>
</html>
"""
with open(output_path, 'w') as f:
f.write(html)
print(f"HTML report generated: {output_path}")
def main():
parser = argparse.ArgumentParser(
description="Generate mutation testing report"
)
parser.add_argument(
"--output",
default="tests/mutation_tests/reports/mutation_report.html",
help="Output path for HTML report"
)
args = parser.parse_args()
# Get results
output = get_mutmut_results()
results = parse_mutmut_results(output) or {'score': 0.0}
# Generate report
output_path = Path(args.output)
output_path.parent.mkdir(parents=True, exist_ok=True)
generate_html_report(results, output_path)
print(f"\nMutation Score: {results['score']:.2f}%")
if __name__ == "__main__":
main()
|