diff --git a/src/hooks/teardown.js b/src/hooks/teardown.js index 881b895..afa691b 100644 --- a/src/hooks/teardown.js +++ b/src/hooks/teardown.js @@ -5,7 +5,14 @@ const testLifecycle = require('./testLifecycle'); * 在所有测试结束后执行 */ async function globalTeardown() { - await testLifecycle.afterAll(); + console.log('开始执行全局清理...'); + try { + await testLifecycle.afterAll(); + console.log('全局清理完成'); + } catch (error) { + console.error('全局清理出错:', error); + throw error; + } } module.exports = globalTeardown; \ No newline at end of file diff --git a/src/hooks/testLifecycle.js b/src/hooks/testLifecycle.js index 30395dc..55ab409 100644 --- a/src/hooks/testLifecycle.js +++ b/src/hooks/testLifecycle.js @@ -10,7 +10,7 @@ const FileUtils = require('../utils/FileUtils'); */ class TestLifecycle { constructor() { - this.reportPath = process.env.PERFORMANCE_REPORT_PATH || './test-results/performance-report.json'; + this.reportPath = process.env.PERFORMANCE_REPORT_PATH || './test-results/performance-report.html'; } /** @@ -25,95 +25,181 @@ class TestLifecycle { console.log('✨ 测试环境初始化完成'); } - /** - * 测试结束后的处理 - * 在所有测试结束后执行,用于生成报告和清理工作 - */ async afterAll() { - const performanceData = performanceService.getPerformanceData(); - const report = this.generateReport(performanceData); - await this.saveReport(report); - console.log(`📊 测试报告已生成: ${this.reportPath}`); + console.log('开始生成测试报告...'); + try { + const performanceData = performanceService.getPerformanceData(); + console.log('获取到性能数据:', performanceData.length, '条记录'); + const report = this.generateReport(performanceData); + await this.saveReport(report); + console.log(`📊 测试报告已生成: ${this.reportPath}`); + } catch (error) { + console.error('生成测试报告出错:', error); + throw error; + } } /** * 生成测试报告 - * @param {Array} performanceData 性能数据数组 - * @returns {Object} 测试报告对象 + * @param {Object} data 性能数据对象 + * @returns {String} 测试报告字符串 */ generateReport(performanceData) { - // 按页面名称分组的统计 + // 统计数据 + const totalTests = performanceData.length; + const successTests = performanceData.filter(record => record.success).length; + const failedTests = totalTests - successTests; + const successRate = totalTests > 0 ? ((successTests / totalTests) * 100).toFixed(2) : '0.00'; + const totalDuration = performanceData.reduce((sum, record) => sum + record.duration, 0); + + // 按页面分组统计 const pageStats = {}; - let totalDuration = 0; - let totalSuccess = 0; - let totalFailure = 0; - - // 统计每个页面的性能数据 performanceData.forEach(record => { - const { pageName, duration, success } = record; - + const { pageName, duration, success, errorMessage } = record; if (!pageStats[pageName]) { pageStats[pageName] = { totalTests: 0, successCount: 0, failureCount: 0, totalDuration: 0, - averageDuration: 0, - minDuration: Infinity, - maxDuration: 0, errors: [] }; } - + const stats = pageStats[pageName]; stats.totalTests++; success ? stats.successCount++ : stats.failureCount++; stats.totalDuration += duration; - stats.minDuration = Math.min(stats.minDuration, duration); - stats.maxDuration = Math.max(stats.maxDuration, duration); - stats.averageDuration = stats.totalDuration / stats.totalTests; - - if (!success && record.errorMessage) { - stats.errors.push({ - timestamp: record.timestamp, - error: record.errorMessage - }); + + if (!success && errorMessage) { + stats.errors.push(errorMessage); } - - // 更新总体统计 - totalDuration += duration; - success ? totalSuccess++ : totalFailure++; }); - // 生成报告 - return { - summary: { - totalTests: performanceData.length, - successCount: totalSuccess, - failureCount: totalFailure, - successRate: (totalSuccess / performanceData.length * 100).toFixed(2) + '%', - averageDuration: totalDuration / performanceData.length, - totalDuration: totalDuration - }, - pageStats, - timestamp: new Date().toISOString(), - rawData: performanceData - }; + // 生成HTML报告 + return ` + + +
+ +执行时间: ${new Date().toLocaleString()}
+${totalTests}
+${successTests}
+${failedTests}
+${successRate}%
+${(totalDuration / 1000).toFixed(2)}秒
+| 页面名称 | +测试数 | +成功数 | +失败数 | +总耗时 | +错误信息 | +
|---|---|---|---|---|---|
| ${pageName} | +${stats.totalTests} | +${stats.successCount} | +${stats.failureCount} | +${(stats.totalDuration / 1000).toFixed(2)}秒 | +
+ ${stats.errors.length > 0 ?
+ ` ${stats.errors.join(' ` :
+ ''}
+ ')} |
+
| 页面名称 | +状态 | +耗时 | +错误信息 | +
|---|---|---|---|
| ${record.pageName} | +${record.success ? '通过' : '失败'} | +${(record.duration / 1000).toFixed(2)}秒 | +${record.errorMessage || ''} | +