chore(junit): render project as an agent name / hostname (#20927)

This commit is contained in:
Pavel Feldman 2023-02-16 07:59:05 -08:00 committed by GitHub
parent 6f140d79d4
commit 96050a260e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View file

@ -18,7 +18,7 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import type { FullConfig, FullResult, Reporter, Suite, TestCase } from '../../types/testReporter'; import type { FullConfig, FullResult, Reporter, Suite, TestCase } from '../../types/testReporter';
import { monotonicTime } from 'playwright-core/lib/utils'; import { monotonicTime } from 'playwright-core/lib/utils';
import { formatFailure, formatTestTitle, stripAnsiEscapes } from './base'; import { formatFailure, stripAnsiEscapes } from './base';
import { assert } from 'playwright-core/lib/utils'; import { assert } from 'playwright-core/lib/utils';
class JUnitReporter implements Reporter { class JUnitReporter implements Reporter {
@ -60,7 +60,7 @@ class JUnitReporter implements Reporter {
const children: XMLEntry[] = []; const children: XMLEntry[] = [];
for (const projectSuite of this.suite.suites) { for (const projectSuite of this.suite.suites) {
for (const fileSuite of projectSuite.suites) for (const fileSuite of projectSuite.suites)
children.push(this._buildTestSuite(fileSuite)); children.push(this._buildTestSuite(projectSuite.title, fileSuite));
} }
const tokens: string[] = []; const tokens: string[] = [];
@ -91,7 +91,7 @@ class JUnitReporter implements Reporter {
} }
} }
private _buildTestSuite(suite: Suite): XMLEntry { private _buildTestSuite(projectName: string, suite: Suite): XMLEntry {
let tests = 0; let tests = 0;
let skipped = 0; let skipped = 0;
let failures = 0; let failures = 0;
@ -106,7 +106,7 @@ class JUnitReporter implements Reporter {
++failures; ++failures;
for (const result of test.results) for (const result of test.results)
duration += result.duration; duration += result.duration;
this._addTestCase(test, children); this._addTestCase(suite.title, test, children);
}); });
this.totalTests += tests; this.totalTests += tests;
this.totalSkipped += skipped; this.totalSkipped += skipped;
@ -115,9 +115,9 @@ class JUnitReporter implements Reporter {
const entry: XMLEntry = { const entry: XMLEntry = {
name: 'testsuite', name: 'testsuite',
attributes: { attributes: {
name: suite.location ? path.relative(this.config.rootDir, suite.location.file) : '', name: suite.title,
timestamp: this.timestamp, timestamp: this.timestamp,
hostname: '', hostname: projectName,
tests, tests,
failures, failures,
skipped, skipped,
@ -130,14 +130,16 @@ class JUnitReporter implements Reporter {
return entry; return entry;
} }
private _addTestCase(test: TestCase, entries: XMLEntry[]) { private _addTestCase(suiteName: string, test: TestCase, entries: XMLEntry[]) {
const entry = { const entry = {
name: 'testcase', name: 'testcase',
attributes: { attributes: {
// Skip root, project, file // Skip root, project, file
name: test.titlePath().slice(3).join(' '), name: test.titlePath().slice(3).join(' '),
classname: formatTestTitle(this.config, test, undefined, true), // filename
classname: suiteName,
time: (test.results.reduce((acc, value) => acc + value.duration, 0)) / 1000 time: (test.results.reduce((acc, value) => acc + value.duration, 0)) / 1000
}, },
children: [] as XMLEntry[] children: [] as XMLEntry[]
}; };

View file

@ -237,7 +237,7 @@ test('should not render projects if they dont exist', async ({ runInlineTest })
expect(xml['testsuites']['testsuite'][0]['$']['failures']).toBe('0'); expect(xml['testsuites']['testsuite'][0]['$']['failures']).toBe('0');
expect(xml['testsuites']['testsuite'][0]['$']['skipped']).toBe('0'); expect(xml['testsuites']['testsuite'][0]['$']['skipped']).toBe('0');
expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['name']).toBe('one'); expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['name']).toBe('one');
expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['classname']).toContain('a.test.js one'); expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['classname']).toBe('a.test.js');
expect(result.exitCode).toBe(0); expect(result.exitCode).toBe(0);
}); });
@ -259,18 +259,20 @@ test('should render projects', async ({ runInlineTest }) => {
expect(xml['testsuites']['testsuite'].length).toBe(2); expect(xml['testsuites']['testsuite'].length).toBe(2);
expect(xml['testsuites']['testsuite'][0]['$']['name']).toBe('a.test.js'); expect(xml['testsuites']['testsuite'][0]['$']['name']).toBe('a.test.js');
expect(xml['testsuites']['testsuite'][0]['$']['hostname']).toBe('project1');
expect(xml['testsuites']['testsuite'][0]['$']['tests']).toBe('1'); expect(xml['testsuites']['testsuite'][0]['$']['tests']).toBe('1');
expect(xml['testsuites']['testsuite'][0]['$']['failures']).toBe('0'); expect(xml['testsuites']['testsuite'][0]['$']['failures']).toBe('0');
expect(xml['testsuites']['testsuite'][0]['$']['skipped']).toBe('0'); expect(xml['testsuites']['testsuite'][0]['$']['skipped']).toBe('0');
expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['name']).toBe('one'); expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['name']).toBe('one');
expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['classname']).toContain('[project1] a.test.js one'); expect(xml['testsuites']['testsuite'][0]['testcase'][0]['$']['classname']).toBe('a.test.js');
expect(xml['testsuites']['testsuite'][1]['$']['name']).toBe('a.test.js'); expect(xml['testsuites']['testsuite'][1]['$']['name']).toBe('a.test.js');
expect(xml['testsuites']['testsuite'][1]['$']['hostname']).toBe('project2');
expect(xml['testsuites']['testsuite'][1]['$']['tests']).toBe('1'); expect(xml['testsuites']['testsuite'][1]['$']['tests']).toBe('1');
expect(xml['testsuites']['testsuite'][1]['$']['failures']).toBe('0'); expect(xml['testsuites']['testsuite'][1]['$']['failures']).toBe('0');
expect(xml['testsuites']['testsuite'][1]['$']['skipped']).toBe('0'); expect(xml['testsuites']['testsuite'][1]['$']['skipped']).toBe('0');
expect(xml['testsuites']['testsuite'][1]['testcase'][0]['$']['name']).toBe('one'); expect(xml['testsuites']['testsuite'][1]['testcase'][0]['$']['name']).toBe('one');
expect(xml['testsuites']['testsuite'][1]['testcase'][0]['$']['classname']).toContain('[project2] a.test.js one'); expect(xml['testsuites']['testsuite'][1]['testcase'][0]['$']['classname']).toBe('a.test.js');
expect(result.exitCode).toBe(0); expect(result.exitCode).toBe(0);
}); });