70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import * as fs from 'fs';
|
|
|
|
interface TestResult {
|
|
title: string;
|
|
status: 'passed' | 'failed' | 'skipped';
|
|
duration: number;
|
|
}
|
|
|
|
interface CoverageReport {
|
|
total: number;
|
|
passed: number;
|
|
failed: number;
|
|
skipped: number;
|
|
avgDuration: number;
|
|
passRate: number;
|
|
}
|
|
|
|
function analyzeTestCoverage(resultsPath: string): CoverageReport {
|
|
if (!fs.existsSync(resultsPath)) {
|
|
console.error(`错误: 找不到测试结果文件 ${resultsPath}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const content = fs.readFileSync(resultsPath, 'utf-8');
|
|
const results = JSON.parse(content);
|
|
|
|
const tests: TestResult[] = results.suites
|
|
.flatMap((suite: any) => suite.specs || [])
|
|
.map((spec: any) => ({
|
|
title: spec.title,
|
|
status: spec.ok ? 'passed' : 'failed',
|
|
duration: spec.duration || 0,
|
|
}));
|
|
|
|
const report: CoverageReport = {
|
|
total: tests.length,
|
|
passed: tests.filter(t => t.status === 'passed').length,
|
|
failed: tests.filter(t => t.status === 'failed').length,
|
|
skipped: tests.filter(t => t.status === 'skipped').length,
|
|
avgDuration: tests.length > 0 ? tests.reduce((sum, t) => sum + t.duration, 0) / tests.length : 0,
|
|
passRate: 0,
|
|
};
|
|
|
|
report.passRate = report.total > 0 ? (report.passed / report.total) * 100 : 0;
|
|
|
|
return report;
|
|
}
|
|
|
|
const resultsPath = process.argv[2] || 'reports/results.json';
|
|
const report = analyzeTestCoverage(resultsPath);
|
|
|
|
console.log('\n=== 测试覆盖率分析 ===');
|
|
console.log(`总测试数: ${report.total}`);
|
|
console.log(`通过: ${report.passed}`);
|
|
console.log(`失败: ${report.failed}`);
|
|
console.log(`跳过: ${report.skipped}`);
|
|
console.log(`通过率: ${report.passRate.toFixed(2)}%`);
|
|
console.log(`平均执行时间: ${(report.avgDuration / 1000).toFixed(2)}秒`);
|
|
|
|
if (!fs.existsSync('reports')) {
|
|
fs.mkdirSync('reports', { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(
|
|
'reports/test-coverage-analysis.json',
|
|
JSON.stringify(report, null, 2)
|
|
);
|
|
|
|
console.log('\n✅ 分析结果已保存到 reports/test-coverage-analysis.json');
|