You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CodeceptJS has two engines for running tests in parallel:
8
+
CodeceptJS has multiple approaches for running tests in parallel:
9
9
10
-
*`run-workers` - which spawns [NodeJS Worker](https://nodejs.org/api/worker_threads.html) in a thread. Tests are split by scenarios, scenarios are mixed between groups, each worker runs tests from its own group.
11
-
*`run-multiple` - which spawns a subprocess with CodeceptJS. Tests are split by files and configured in `codecept.conf.js`.
10
+
-**Test Sharding** - distributes tests across multiple machines for CI matrix execution
11
+
-`run-workers` - which spawns [NodeJS Worker](https://nodejs.org/api/worker_threads.html) in a thread. Tests are split by scenarios, scenarios are mixed between groups, each worker runs tests from its own group.
12
+
-`run-multiple` - which spawns a subprocess with CodeceptJS. Tests are split by files and configured in `codecept.conf.js`.
12
13
13
14
Workers are faster and simpler to start, while `run-multiple` requires additional configuration and can be used to run tests in different browsers at once.
14
15
16
+
## Test Sharding for CI Matrix
17
+
18
+
Test sharding allows you to split your test suite across multiple machines or CI workers without manual configuration. This is particularly useful for CI/CD pipelines where you want to run tests in parallel across different machines.
19
+
20
+
Use the `--shard` option with the `run` command to execute only a portion of your tests:
21
+
22
+
```bash
23
+
# Run the first quarter of tests
24
+
npx codeceptjs run --shard 1/4
25
+
26
+
# Run the second quarter of tests
27
+
npx codeceptjs run --shard 2/4
28
+
29
+
# Run the third quarter of tests
30
+
npx codeceptjs run --shard 3/4
31
+
32
+
# Run the fourth quarter of tests
33
+
npx codeceptjs run --shard 4/4
34
+
```
35
+
36
+
### CI Matrix Example
37
+
38
+
Here's how you can use test sharding with GitHub Actions matrix strategy:
39
+
40
+
```yaml
41
+
name: Tests
42
+
on: [push, pull_request]
43
+
44
+
jobs:
45
+
test:
46
+
runs-on: ubuntu-latest
47
+
strategy:
48
+
matrix:
49
+
shard: [1/4, 2/4, 3/4, 4/4]
50
+
51
+
steps:
52
+
- uses: actions/checkout@v2
53
+
- uses: actions/setup-node@v2
54
+
- run: npm install
55
+
- run: npx codeceptjs run --shard ${{ matrix.shard }}
56
+
```
57
+
58
+
This approach ensures:
59
+
60
+
- Each CI job runs only its assigned portion of tests
61
+
- Tests are distributed evenly across shards
62
+
- No manual configuration or maintenance of test lists
63
+
- Automatic load balancing as you add or remove tests
64
+
65
+
### Shard Distribution
66
+
67
+
Tests are distributed evenly across shards using a round-robin approach:
68
+
69
+
- If you have 100 tests and 4 shards, each shard runs approximately 25 tests
70
+
- The first shard gets tests 1-25, second gets 26-50, third gets 51-75, fourth gets 76-100
71
+
- If tests don't divide evenly, earlier shards may get one extra test
72
+
15
73
## Parallel Execution by Workers
16
74
17
75
It is easy to run tests in parallel if you have a lots of tests and free CPU cores. Just execute your tests using `run-workers` command specifying the number of workers to spawn:
CodeceptJS also exposes the env var `process.env.RUNS_WITH_WORKERS` when running tests with `run-workers` command so that you could handle the events better in your plugins/helpers
129
187
130
188
```js
131
-
const { event } =require('codeceptjs');
189
+
const { event } =require('codeceptjs')
132
190
133
-
module.exports=function() {
134
-
// this event would trigger the `_publishResultsToTestrail` when running `run-workers` command
191
+
module.exports=function() {
192
+
// this event would trigger the `_publishResultsToTestrail` when running `run-workers` command
// when running `run` command, this env var is undefined
142
-
if (!process.env.RUNS_WITH_WORKERS) await_publishResultsToTestrail();
143
-
});
199
+
// when running `run` command, this env var is undefined
200
+
if (!process.env.RUNS_WITH_WORKERS) await_publishResultsToTestrail()
201
+
})
144
202
}
145
203
```
146
204
147
205
## Parallel Execution by Workers on Multiple Browsers
148
206
149
-
To run tests in parallel across multiple browsers, modify your `codecept.conf.js` file to configure multiple browsers on which you want to run your tests and your tests will run across multiple browsers.
207
+
To run tests in parallel across multiple browsers, modify your `codecept.conf.js` file to configure multiple browsers on which you want to run your tests and your tests will run across multiple browsers.
150
208
151
-
Start with modifying the `codecept.conf.js` file. Add multiple key inside the config which will be used to configure multiple profiles.
209
+
Start with modifying the `codecept.conf.js` file. Add multiple key inside the config which will be used to configure multiple profiles.
152
210
153
211
```
154
212
exports.config = {
@@ -174,7 +232,7 @@ exports.config = {
174
232
}
175
233
}
176
234
]
177
-
},
235
+
},
178
236
profile2: {
179
237
browsers: [
180
238
{
@@ -188,16 +246,21 @@ exports.config = {
188
246
}
189
247
};
190
248
```
191
-
To trigger tests on all the profiles configured, you can use the following command:
249
+
250
+
To trigger tests on all the profiles configured, you can use the following command:
251
+
192
252
```
193
253
npx codeceptjs run-workers 3 all -c codecept.conf.js
194
254
```
255
+
195
256
This will run your tests across all browsers configured from profile1 & profile2 on 3 workers.
196
257
197
-
To trigger tests on specific profile, you can use the following command:
258
+
To trigger tests on specific profile, you can use the following command:
If you want your tests to split according to your need this method is suited for you. For example: If you have 4 long running test files and 4 normal test files there chance all 4 tests end up in same worker thread. For these cases custom function will be helpful.
314
377
315
378
```js
316
-
317
379
/*
318
380
Define a function to split your tests.
319
381
@@ -322,28 +384,25 @@ If you want your tests to split according to your need this method is suited for
322
384
where file1 and file2 will run in a worker thread and file3 will run in a worker thread
0 commit comments