How to Run Cypress Tests in Parallel
By Ganesh Hegde, Community Contributor - January 27, 2022
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. Testers can access a real device cloud of 3000+ real browsers and devices. It empowers teams to run concurrent Selenium, Appium, or Cypress tests on desired real device-browser combinations online. Without the limitations of emulators and simulators, testers can verify website functionality in real user conditions.
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.
- Test Infrastructure: 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 should be designed to handle the load and execute test cases smoothly.
- Execute Tests on CI/CD pipelines: To yield the best possible output, either create the infra for running parallel tests on CI/CD or opt for a ready-made solution like BrowserStack which offers in-built CI/CD integration to run parallel tests.
Now, let’s move on to parallel testing in Cypress.
How to run Cypress Tests in Parallel
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
- Install Browserstack CLI
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.
- BrowserStack provides two options to run Cypress Tests parallelly
1. Using browserstack.json file
2. Using CLI Mode
Option 1: Using browserstack.json file
- Create browserstack.json file in your Project Folder
Note: We recommend browserstack.json should be kept in the same place where the cypress.json file is located.
- Feed the configuration information to the browserstack.json file
The browserstack.json file contains information regarding the browser and OS combinations to be tested on.
You can copy the default configuration from here.
- Set up the parallels attribute in your browserstack.json file
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.
- Execute Cypress tests parallelly
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.