How To Execute Tests Parallelly using Cypress And GitHub Actions

Knoldus Inc.
4 min readApr 26, 2022

--

Hello Readers!!
In today’s world when the time is money, it is very important to lower the operational time for various actions. The same goes with test execution. One of the popular ways to achieve execution of more test cases in lesser time is by parallel test execution. Before learning how to execute tests parallelly using Cypress, let’s have a walkthrough of Parallel Test Execution and Cypress.

What is Parallel Test Execution?

As the name itself suggests, the parallel test execution is the execution of test cases parallelly. This means that more than one test case is executed at a single instance of time rather than being executed one after another.
Parallel Test execution has gained momentum with increased demand for cross-browser testing in the market today. It allows the testers to run the test cases parallelly on different browsers. Thus, saving lots of time and effort for the testers.

Cypress: End To End Testing Framework

Cypress is a JavaScript-based end to end testing framework. Built on the top of Mocha which is a Javascript-based test framework. It is a free and open-source automation tool.
Cypress provides a test scripts runner along with visual stimulation of test cases execution. This helps the tester know the behaviour of the application under test at each test step.

Parallel Test execution with Cypress

Cypress helps to run tests parallelly with ease by executing your test cases on CI Tools. The tester can also execute the test cases parallelly on the local device. But, it is not recommended as it will require more resources and slow down the CPU. It is always beneficial if you run it by setting up the parallel execution on the CI tools.

In this blog, we are going to use Github Actions to run our test cases parallelly. Below are the steps that one needs to follow for Cypress Parallel Test Execution.

Steps to execute tests parallelly using Cypress

Create a Cypress Project

  • Create a Cypress Project
  • Separate the two spec.js files for running it parallelly on the CI tool.
execute tests parallelly using cypress: cypress project

Integrate with Cypress Dashboard

  • Login into the Cypress dashboard and create a new project
  • Find the project id in project settings and add it to the cypress.json file in the below manner.
execute tests parallelly using cypress : cypress.json fle
  • Fetch your record key in project settings and add it to GitHub Secrets. Here, we have used the variable: RECORD_KEY to store the value.
execute tests parallelly using cypress : dashboard project settings

Add a YAML file for parallel execution

  • Create a .gitub/workflows directory
  • Add a YAML file with the following specifications
name: Parallel Cypress Tests

on: [push]

jobs:
test:
name: Cypress run
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
# run 3 copies of the current job in parallel
containers: [1, 2, 3]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cypress run
uses: cypress-io/github-action@v2
with:
record: true
parallel: true
group: 'Actions example'
env:
CYPRESS_RECORD_KEY: ${{ secrets.RECORD_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  • Push your code into the GitHub repo.
  • Move to the Actions tab and open the latest build
  • Here, you will see your test cases running parallelly on two machines.
execute tests parallelly using cypress: github actions build

Here, the two test cases have been executed parallelly at the same time. Checking the history of both the jobs, we can see that login1.spec.js has been executed on job 1 and login2.spec.js has been executed on job 2.

execute tests parallelly using cypress: job 1 with login1.spec.js
github actions: cypress job 2

Conclusion

Apart from GitHub Actions, Cypress also integrates with CircleCI, GitLab etc. to implement continuous testing and parallelization.
Parallel Execution in Cypress is very beneficial as it accelerates the process of continuous testing. It aids in running a large number of test cases in lesser time.

--

--

Knoldus Inc.

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com