How to Run Cypress Tests in Parallel
Before diving into how to run parallel tests in Cypress, let’s get a quick overview of parallel testing in general .
What is Parallel Testing?
Parallel Testing is a commonly used term in the automation testing domain. Instead of running tests one by one in sequence, parallel testing allows for running multiple tests simultaneously. In this approach, tests are executed on multiple browsers, devices, and operating systems parallelly.
Advantages of Parallel Testing
- Faster Execution: Executes tests in different instances machines simultaneously, reducing execution time and returning test results much quicker.
- Less Effort: When parallel testing is triggered, testers do not have to manually wait for one test to complete and trigger another test on a different browser. Tests run on multiple browser-device-OS combinations, so capturing results with less effort is easy.
- Better Test Coverage: Parallel testing allows for running tests on as many device-browser-OS combinations as the tester’s infrastructure and budget will allow. This will help attain maximum test coverage without significantly increasing execution time or compromising the accuracy of results.
Challenges of Parallel Testing
The major challenge in parallel testing is setting up infrastructure to execute said tests. Running multiple browser instances on a single machine can eventually overload the machine, eventually returning false failure results.
Accessing different combinations of real browsers, devices (especially mobile), and operating systems would also be a challenge, especially for teams that don’t have an in-house device lab (consistently maintained and updated).
Of course, it gets a lot easier for QAs to perform parallel tests when they use a cloud-based testing platform such as BrowserStack.
Best Practices for Parallel Testing
- Keep Tests Independent: Create tests that are as independent as possible. Dependencies are major road blockers for parallel tests and increase failure rates.
- Focus on Test Data Management: Ensure that data values are refreshed inside test cases. If one test case alters the values of other test cases, that will eventually impact test results.
- Ensure Testing is carried out on Real Browsers and Devices: There is always the possibility that emulators and simulators will provide false results so it is always best to test on real browsers and devices.
- When testers run multiple combinations of tests parallelly, a certain load will be generated on the machine, depending on the number and nature of parallel tests. The Test Infrastructure:test infrastructure should be designed to handle the load and execute test cases smoothly.
- To yield the best possible output, either create the infra for running parallel tests on CI/CD.
Now, let’s move on to parallel testing in Cypress.
How to run Cypress Tests in Parallel
Cypress restricts running tests parallelly on a local machine. The framework was explicitly designed to do this, and multiple reasons for this can be found in the Cypress official .
Cypress provides a Parallel Testing option by setting up multiple machines.
To get started, one has to set up a parallel testing strategy per the needs of a particular project. Depending on the project and personnel, this process can be complicated and time-consuming. Another major challenge is maintaining the test infrastructure unless one utilizes a test-ready environment like BrowserStack.
This article will demonstrate parallel testing in Cypress via the BrowserStack cloud.
Running Cypress Tests on Browserstack
Since Cypress supports parallel testing only in multiple virtual machine instances, you need to set up the whole environment and make Cypress execute your tests. But the best approach is to use cloud-based platform testing solutions since they have already set up the environment. Real browsers and devices are ready and waiting on the cloud; you just need to create test scripts and trigger the tests.
BrowserStack allows you to run Cypress tests on the latest browsers. Get 30+ versions across Windows and macOS, with more to come. You can also run hundreds of Cypress tests in parallel without maintaining countless Docker images. It takes a few easy steps to start running Cypress tests on BrowserStack.
Step by Step Guide to Execute Cypress Tests Parallelly on BrowserStack
Navigate to your Project Folder and execute the command below:
npm install -g BrowserStack-cypress-cli
The above command installs all necessary packages required to run Cypress tests in parallel on BrowserStack.
1. Using browserstack.json file
2. Using CLI Mode
Option 1: Using browserstack.json file Note: We recommend browserstack.json should be kept in the same place where the cypress.json file is located.
The browserstack.json file contains information regarding the browser and OS combinations to be tested on.
You can copy the default configuration from .
The run settings in the browserstack.json file have a “parallels”: option; you can specify the number of parallel instances here. The default is 5.
Using the command below, you can start Cypress tests on BrowserStack
browserstack-cypress run -sync
Once tests are executed, the results are shown in both the Command Line as well as the BrowserStack Dashboard — as seen in the image below:
Option 2: Using CLI
If you are not looking for a specific configuration and just want to run your tests parallelly, try the easy option by mentioning command-line flags parallels. (Skip the steps for Option 1). Use the command below:
browserstack-cypress run --sync --parallels 5
Once you execute, you can see results in the command line, as shown below:
Leverage the various advantages of Cypress to run parallel tests effortlessly. Use BrowserStack’s real browsers to ensure that all tests return 100% accurate results, even when executing multiple tests at the same time. Don’t limit your tests with the various inadequacies of emulators and simulators; only rely on the real deal to create customer-ready, meticulously optimized web applications.
Originally published at https://www.browserstack.com.