Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test-runner): list mode should print tests (#7665)

Dmitry Gozman602d81598184
diff --git a/src/test/runner.ts b/src/test/runner.ts
index cb895699ff..83725964af 100644
--- a/src/test/runner.ts
+++ b/src/test/runner.ts
@@ -34,7 +34,7 @@ import JUnitReporter from './reporters/junit';
import EmptyReporter from './reporters/empty';
import { ProjectImpl } from './project';
import { Minimatch } from 'minimatch';
-import { Config } from './types';
+import { Config, FullConfig } from './types';
import { LaunchServers } from './launchServer';
const removeFolderAsync = promisify(rimraf);
@@ -93,7 +93,7 @@ export class Runner {
}
async run(list: boolean, filePatternFilters: FilePatternFilter[], projectName?: string): Promise<RunResultStatus> {
- this._reporter = await this._createReporter();
+ this._reporter = list ? new ListModeReporter() : await this._createReporter();
const config = this._loader.fullConfig();
const globalDeadline = config.globalTimeout ? config.globalTimeout + monotonicTime() : undefined;
const { result, timedOut } = await raceAgainstDeadline(this._run(list, filePatternFilters, projectName), globalDeadline);
@@ -408,3 +408,20 @@ function getClashingTestsPerSuite(rootSuite: Suite): Map<string, Test[]> {
function buildItemLocation(rootDir: string, testOrSuite: Suite | Test) {
return `${path.relative(rootDir, testOrSuite.location.file)}:${testOrSuite.location.line}`;
}
+
+class ListModeReporter implements Reporter {
+ onBegin(config: FullConfig, suite: Suite): void {
+ console.log(`Listing tests:`);
+ const tests = suite.allTests();
+ const files = new Set<string>();
+ for (const test of tests) {
+ // root, project, file, ...describes, test
+ const [, projectName, , ...titles] = test.titlePath();
+ const location = `${path.relative(config.rootDir, test.location.file)}:${test.location.line}:${test.location.column}`;
+ const projectTitle = projectName ? `[${projectName}] › ` : '';
+ console.log(` ${projectTitle}${location} › ${titles.join(' ')}`);
+ files.add(test.location.file);
+ }
+ console.log(`Total: ${tests.length} ${tests.length === 1 ? 'test' : 'tests'} in ${files.size} ${files.size === 1 ? 'file' : 'files'}`);
+ }
+}
diff --git a/src/test/test.ts b/src/test/test.ts
index e3248bde1a..11f79af7d4 100644
--- a/src/test/test.ts
+++ b/src/test/test.ts
@@ -139,7 +139,6 @@ export class Test extends Base implements reporterTypes.Test {
}
status(): 'skipped' | 'expected' | 'unexpected' | 'flaky' {
- // List mode bail out.
if (!this.results.length)
return 'skipped';
if (this.results.length === 1 && this.expectedStatus === this.results[0].status)
diff --git a/tests/playwright-test/json-reporter.spec.ts b/tests/playwright-test/json-reporter.spec.ts
index 1b362f0456..90064c2f61 100644
--- a/tests/playwright-test/json-reporter.spec.ts
+++ b/tests/playwright-test/json-reporter.spec.ts
@@ -108,3 +108,19 @@ test('should report projects', async ({ runInlineTest }, testInfo) => {
expect(result.report.suites[0].specs[0].tests[0].projectName).toBe('p1');
expect(result.report.suites[0].specs[0].tests[1].projectName).toBe('p2');
});
+
+test('should have relative always-posix paths', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.test.js': `
+ const { test } = pwt;
+ test('math works!', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ `
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.report.config.rootDir.indexOf(path.win32.sep)).toBe(-1);
+ expect(result.report.suites[0].specs[0].file).toBe('a.test.js');
+ expect(result.report.suites[0].specs[0].line).toBe(6);
+ expect(result.report.suites[0].specs[0].column).toBe(7);
+});
diff --git a/tests/playwright-test/list-mode.spec.ts b/tests/playwright-test/list-mode.spec.ts
index b1fb98369f..1c7f6435cf 100644
--- a/tests/playwright-test/list-mode.spec.ts
+++ b/tests/playwright-test/list-mode.spec.ts
@@ -14,21 +14,30 @@
* limitations under the License.
*/
-import path from 'path';
import { test, expect } from './playwright-test-fixtures';
-test('should have relative always-posix paths', async ({ runInlineTest }) => {
+test('should list tests', async ({ runInlineTest }) => {
const result = await runInlineTest({
+ 'playwright.config.ts': `
+ module.exports = { projects: [{ name: 'foo' }, {}] };
+ `,
'a.test.js': `
const { test } = pwt;
- test('math works!', async ({}) => {
+ test('example1', async ({}) => {
+ expect(1 + 1).toBe(2);
+ });
+ test('example2', async ({}) => {
expect(1 + 1).toBe(2);
});
`
}, { 'list': true });
expect(result.exitCode).toBe(0);
- expect(result.report.config.rootDir.indexOf(path.win32.sep)).toBe(-1);
- expect(result.report.suites[0].specs[0].file).toBe('a.test.js');
- expect(result.report.suites[0].specs[0].line).toBe(6);
- expect(result.report.suites[0].specs[0].column).toBe(7);
+ expect(result.output).toContain([
+ `Listing tests:`,
+ ` [foo] › a.test.js:6:7 › example1`,
+ ` [foo] › a.test.js:9:7 › example2`,
+ ` a.test.js:6:7 › example1`,
+ ` a.test.js:9:7 › example2`,
+ `Total: 4 tests in 1 file`
+ ].join('\n'));
});