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
| // tools/test-reporter/reporter.js
const MochaReporter = require('mocha/lib/reporters/base');
const fs = require('fs');
const path = require('path');
class GameTestReporter extends MochaReporter {
constructor(runner, options) {
super(runner, options);
this.results = {
suite: '',
tests: [],
passed: 0,
failed: 0,
skipped: 0,
duration: 0,
timestamp: new Date().toISOString()
};
// 监听事件
runner.on('start', () => this.onTestStart());
runner.on('pass', test => this.onTestPass(test));
runner.on('fail', (test, err) => this.onTestFail(test, err));
runner.on('end', () => this.onTestEnd());
}
onTestStart() {
this.results.timestamp = new Date().toISOString();
}
onTestPass(test) {
this.results.passed++;
this.results.tests.push({
title: test.fullTitle(),
status: 'passed',
duration: test.duration,
retries: test.retries
});
}
onTestFail(test, err) {
this.results.failed++;
this.results.tests.push({
title: test.fullTitle(),
status: 'failed',
duration: test.duration,
error: {
message: err.message,
stack: err.stack,
actual: err.actual,
expected: err.expected
}
});
}
onTestEnd() {
this.results.duration = this.stats.duration;
// 生成报告
this.generateReports();
}
generateReports() {
// JSON 报告
const jsonPath = path.join('test-results', 'results.json');
fs.mkdirSync(path.dirname(jsonPath), { recursive: true });
fs.writeFileSync(jsonPath, JSON.stringify(this.results, null, 2));
// HTML 报告
const htmlPath = path.join('test-results', 'index.html');
const html = this.generateHTMLReport();
fs.writeFileSync(htmlPath, html);
// JUnit XML 报告
const xmlPath = path.join('test-results', 'junit.xml');
const xml = this.generateJUnitReport();
fs.writeFileSync(xmlPath, xml);
}
generateHTMLReport() {
return `
<!DOCTYPE html>
<html>
<head>
<title>Test Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.summary { background: #f5f5f5; padding: 20px; margin-bottom: 20px; }
.passed { color: green; }
.failed { color: red; }
.test { border: 1px solid #ddd; padding: 10px; margin: 10px 0; }
.test.passed { border-left: 4px solid green; }
.test.failed { border-left: 4px solid red; }
.error { background: #ffebee; padding: 10px; margin-top: 10px; }
</style>
</head>
<body>
<h1>Test Report</h1>
<div class="summary">
<h2>Summary</h2>
<p>Total: ${this.results.tests.length}</p>
<p class="passed">Passed: ${this.results.passed}</p>
<p class="failed">Failed: ${this.results.failed}</p>
<p>Duration: ${this.results.duration}ms</p>
</div>
${this.results.tests.map(test => `
<div class="test ${test.status}">
<h3>${test.title}</h3>
<p>Status: ${test.status}</p>
<p>Duration: ${test.duration}ms</p>
${test.error ? `
<div class="error">
<strong>Error:</strong>
<pre>${test.error.message}</pre>
<pre>${test.error.stack}</pre>
</div>
` : ''}
</div>
`).join('')}
</body>
</html>
`;
}
generateJUnitReport() {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="${this.results.tests.length}" failures="${this.results.failed}" time="${this.results.duration / 1000}">
${this.results.tests.map(test => `
<testcase name="${test.title}" time="${test.duration / 1000}">
${test.status === 'failed' ? `
<failure message="${this.escapeXML(test.error.message)}">
${this.escapeXML(test.error.stack)}
</failure>
` : ''}
</testcase>
`).join('')}
</testsuite>
</testsuites>
`;
return xml;
}
escapeXML(str) {
return str.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
}
module.exports = GameTestReporter;
|