Files
everything-is-suitable/scripts/report-generators/junit-report-generator.ts
T
张翔 08ea5fbe98 feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
2026-03-28 14:37:29 +08:00

83 lines
2.4 KiB
TypeScript

import {
BaseReportGenerator,
TestReport,
ReportGeneratorOptions,
} from './base-report-generator';
export class JUnitReportGenerator extends BaseReportGenerator {
constructor(options: ReportGeneratorOptions) {
super(options);
}
getExtension(): string {
return 'xml';
}
generate(report: TestReport): string {
const xml = this.generateJUnitXML(report);
return this.writeToFile(xml);
}
private generateJUnitXML(report: TestReport): string {
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>\n';
const testsuites = this.generateTestSuitesXML(report);
return xmlHeader + testsuites;
}
private generateTestSuitesXML(report: TestReport): string {
const testsuites = report.testSuites
.map(suite => this.generateTestSuiteXML(suite))
.join('\n');
return `<testsuites name="${this.escapeXML(report.reportName)}" tests="${report.totalTests}" failures="${report.totalFailed}" skipped="${report.totalSkipped}" time="${report.totalDuration / 1000}" timestamp="${report.timestamp}">
${testsuites}
</testsuites>`;
}
private generateTestSuiteXML(suite: any): string {
const testcases = suite.tests
.map((test: any) => this.generateTestCaseXML(test))
.join('\n');
const failedTests = suite.tests.filter((t: any) => t.status === 'failed');
return ` <testsuite name="${this.escapeXML(suite.suiteName)}" tests="${suite.tests.length}" failures="${suite.failed}" skipped="${suite.skipped}" time="${suite.duration / 1000}" timestamp="${suite.timestamp}">
${testcases}
</testsuite>`;
}
private generateTestCaseXML(test: any): string {
const testcase = ` <testcase name="${this.escapeXML(test.testName)}" classname="${this.escapeXML(test.testId)}" time="${test.duration / 1000}"`;
if (test.status === 'skipped') {
return `${testcase}>
<skipped/>
</testcase>`;
}
if (test.status === 'failed') {
const failureMessage = test.error
? this.escapeXML(test.error)
: 'Test failed';
const failureDetails = test.stackTrace
? this.escapeXML(test.stackTrace)
: '';
return `${testcase}>
<failure message="${failureMessage}">${failureDetails}</failure>
</testcase>`;
}
return `${testcase}/>`;
}
private escapeXML(str: string): string {
return str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}
}