I hope that everyone already knows the value of test automation and how much time it may save during new feature release. I agree that we have to develop these tests first, but when they are done, they provide a lot of benefits to daily work. What If I said that there is a way to improve test automation by running them parallelly? This blog describes two different ways to run Cypress tests in parallel.
Parallel testing refers to running tests simultaneously across multiple threads of processes. This approach enables tests to execute concurrently rather than one after another in sequential order. In this method, tests are divided into separate groups or batches and distributed among different threads to run independently and in isolation. This method significantly reduces the total test execution time, as it is divided among the available threads. Increasing the number of threads can further accelerate the overall execution process.
To demonstrate how much time can be saved thanks to parallel testing, four simple tests were executed concurrently. The image below shows the achieved results. Dividing the tests into four threads allows to save 36% of the time that would have been required to execute these tests sequentially. In this case ~9 seconds it's not a lot but this is just an example. We should look more broadly and think about how much time parallel testing may save when we have to run thousands of tests twice a week.
To run tests in parallel on the local machine, we can use the open-source library: cypress-parallel. It may be installed by the following command:
npm i --save-dev cypress-parallel
The most popular way to generate a report based on tests results is provided by cypress-mochawesome-reporter. It can be installed by:
npm i --save-dev cypress-mochawesome-reporter
In the next step, we need to configure a reporter for cypress-parallel. We want to generate a report based on the result from each thread. First, we will make some changes in the file: cypress.config.js
For sequentially tests run in setupNodeEvents we need to add
require('cypress-mochawesome-reporter/plugin')(on)
But for cypress-parallel, this section should look different:
setupNodeEvents(on, config) {
on('before:run', async (details) => {
await beforeRunHook(details);
});
}
Above defineConfig, we need to define constant beforeRunHook
const {beforeRunHook} = require(“cypress-mochawesome-reporter/lib”)
In the last step, we will create scripts in our file named: package.json It will make the start procedure more comfortable.
"scripts": {
"cy:run": "cypress run",
"cy:parallel": "cypress-parallel -s cy:run -t 4 -d 'cypress/e2e/parallel-tests/*.js' -r 'cypress-mochawesome-reporter' -o 'cypressParallel=true, saveJson=true, reportTitle=Sample Parallel Title'",
"clean": "rimraf cypress/reports",
"generate-report": "generate-mochawesome-report",
"cy:e2e:parallel": "npm run clean && (npm run cy:parallel || true) && npm run generate-report"
}
Description:
Please refer to the official documentation for the rest of the parameters
Now to run our test we only have to run the below command:
npm run cy:e2e:parallel
The above quick guide was created based on official documentation of linked libraries and this example. Additionally it shows that parallel test configuration doesn't require much effort and allows us to save a lot of hours during testing.
Continuous Integration (CI) - involves automating the process of merging code changes from various contributors into a unified software project. It enabling developers to regularly integrate their updates into a shared repository. Once integrated, builds and tests are automatically triggered to ensure the new code is functional and compatible. This process relies on automated tools to verify the correctness of the code before it becomes part of the main project. Cypress tests can be configured to run using platforms such as GitHub Actions, GitLab, or AWS CodeBuild. For detailed instructions and more examples, refer to the Cypress documentation.
Using Continuous Integration (CI), we can run our tests on multiple machines and browsers simultaneously, making it an excellent choice when our project includes a large number of tests. Each CI has its own configuration steps, so it is necessary to refer to documentation specific to the platform that we are using.
Is a dedicated app that provides many features to improve daily work with tests run in CI.
For details about each benefit review documentation.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 46 | |
| 40 | |
| 38 | |
| 35 | |
| 30 | |
| 28 | |
| 27 | |
| 24 | |
| 24 | |
| 22 |