feat(reporter): Add parallelIndex field to TestResult (#19570)

This commit is contained in:
Vladimir Semenov 2022-12-20 02:37:04 +04:00 committed by GitHub
parent 41174e74a7
commit 467d9f37fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 2 deletions

View file

@ -78,3 +78,9 @@ List of steps inside this test run.
Index of the worker where the test was run. If the test was not run a single time, for example when the user interrupted testing, the only result will have a `workerIndex` equal to `-1`.
Learn more about [parallelism and sharding](../test-parallel.md) with Playwright Test.
## property: TestResult.parallelIndex
* since: v1.30
- type: <[int]>
The index of the worker between `0` and `workers - 1`. It is guaranteed that workers running at the same time have a different `parallelIndex`.

View file

@ -199,6 +199,7 @@ export class Dispatcher {
const data = this._testById.get(params.testId)!;
const result = data.test._appendTestResult();
data.resultByWorkerIndex.set(worker.workerIndex, { result, stepStack: new Set(), steps: new Map() });
result.parallelIndex = worker.parallelIndex;
result.workerIndex = worker.workerIndex;
result.startTime = new Date(params.startWallTime);
this._reporter.onTestBegin?.(data.test, result);
@ -495,7 +496,7 @@ let lastWorkerIndex = 0;
class Worker extends EventEmitter {
private process: child_process.ChildProcess;
private _hash: string;
private parallelIndex: number;
readonly parallelIndex: number;
readonly workerIndex: number;
private _didSendStop = false;
private _didFail = false;

View file

@ -212,6 +212,7 @@ export class TestCase extends Base implements reporterTypes.TestCase {
_appendTestResult(): reporterTypes.TestResult {
const result: reporterTypes.TestResult = {
retry: this.results.length,
parallelIndex: -1,
workerIndex: -1,
duration: 0,
startTime: new Date(),

View file

@ -282,7 +282,13 @@ export interface TestResult {
*
* Learn more about [parallelism and sharding](https://playwright.dev/docs/test-parallel) with Playwright Test.
*/
workerIndex: number;}
workerIndex: number;
/**
* The index of the worker between `0` and `workers - 1`. It is guaranteed that workers running at the same time have
* a different `parallelIndex`.
*/
parallelIndex: number;}
/**
* Result of the full test run.

View file

@ -607,6 +607,29 @@ var import_test = __toModule(require("@playwright/test"));
expect(result.output).toContain('a.spec.ts');
});
test('parallelIndex is presented in onTestEnd', async ({ runInlineTest }) => {
const result = await runInlineTest({
'reporter.ts': `
class Reporter {
onTestEnd(test, result) {
console.log('parallelIndex: ' + result.parallelIndex)
}
}
module.exports = Reporter;`,
'playwright.config.ts': `
module.exports = {
reporter: './reporter',
};
`,
'a.spec.js': `
pwt.test('test', () => {});
`,
}, { 'reporter': '', 'workers': 1 });
expect(result.output).toContain('parallelIndex: 0');
});
function stripEscapedAscii(str: string) {
return str.replace(/\\u00[a-z0-9][a-z0-9]\[[^m]+m/g, '');
}