-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat(test-runner): add --shuffle to run tests in random order #42877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Pavel Feldman (pavelfeldman)
merged 1 commit into
microsoft:main
from
pavelfeldman:fix-9297
Sep 23, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * Copyright (c) Microsoft Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { test, expect } from './playwright-test-fixtures'; | ||
|
|
||
| const count = 10; | ||
|
|
||
| function testFile(name: string) { | ||
| return ` | ||
| import { test } from '@playwright/test'; | ||
| ${Array.from({ length: count }, (_, i) => `test('test${i}', () => console.log('\\n%%${name}-test${i}'));`).join('\n')} | ||
| `; | ||
| } | ||
|
|
||
| const fileNames = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; | ||
| const files = Object.fromEntries(fileNames.map(name => [`${name}.spec.ts`, testFile(name)])); | ||
| const testsInFile = (name: string) => Array.from({ length: count }, (_, i) => `${name}-test${i}`); | ||
| const naturalOrder = fileNames.flatMap(testsInFile); | ||
|
|
||
| test('should shuffle files and keep tests within a file in order', async ({ runInlineTest }) => { | ||
| const result = await runInlineTest(files, { workers: 1, shuffle: '42' }); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.passed).toBe(naturalOrder.length); | ||
| expect(result.output).toContain('shuffle seed 42'); | ||
| expect(result.outputLines).not.toEqual(naturalOrder); | ||
| const fileOrder = [...new Set(result.outputLines.map(line => line.split('-')[0]))]; | ||
| expect(result.outputLines).toEqual(fileOrder.flatMap(testsInFile)); | ||
|
|
||
| const result2 = await runInlineTest(files, { workers: 1, shuffle: '42' }); | ||
| expect(result2.outputLines).toEqual(result.outputLines); | ||
|
|
||
| const result3 = await runInlineTest(files, { workers: 1, shuffle: '43' }); | ||
| expect(result3.outputLines).not.toEqual(result.outputLines); | ||
| }); | ||
|
|
||
| test('should shuffle individual tests in fully parallel mode', async ({ runInlineTest }) => { | ||
| const result = await runInlineTest(files, { 'workers': 1, 'shuffle': '42', 'fully-parallel': true }); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.passed).toBe(naturalOrder.length); | ||
| const aTests = result.outputLines.filter(line => line.startsWith('a-')); | ||
| expect(aTests).not.toEqual(testsInFile('a')); | ||
| expect([...aTests].sort()).toEqual([...testsInFile('a')].sort()); | ||
| }); | ||
|
|
||
| test('should pick and print a random seed', async ({ runInlineTest }) => { | ||
| const result = await runInlineTest(files, { workers: 1, shuffle: true }); | ||
| expect(result.exitCode).toBe(0); | ||
| const seed = result.output.match(/shuffle seed (\d+)/)![1]; | ||
| const result2 = await runInlineTest(files, { workers: 1, shuffle: seed }); | ||
| expect(result2.outputLines).toEqual(result.outputLines); | ||
| }); | ||
|
|
||
| test('should keep serial suites together in parallel mode', async ({ runInlineTest }) => { | ||
| const result = await runInlineTest({ | ||
| 'a.spec.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test.describe.configure({ mode: 'parallel' }); | ||
| ${Array.from({ length: count }, (_, i) => `test('test${i}', () => console.log('\\n%%test${i}'));`).join('\n')} | ||
| test.describe.serial('serial', () => { | ||
| let counter = 0; | ||
| ${Array.from({ length: count }, (_, i) => `test('serial${i}', () => { expect(counter++).toBe(${i}); console.log('\\n%%serial${i}'); });`).join('\n')} | ||
| }); | ||
| `, | ||
| }, { workers: 1, shuffle: '7' }); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.passed).toBe(2 * count); | ||
| const lines = result.outputLines; | ||
| const serialStart = lines.indexOf('serial0'); | ||
| expect(lines.slice(serialStart, serialStart + count)).toEqual(Array.from({ length: count }, (_, i) => `serial${i}`)); | ||
| expect(lines.filter(line => line.startsWith('test'))).not.toEqual(Array.from({ length: count }, (_, i) => `test${i}`)); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests with beforeAll/afterAll can be placed in the same group and won't be shuffled with this approach. Maybe it's okay but perhaps you want to shuffle them too?