Running Cypress Tests in Parallel using GitHub Actions [shorts]

Cypress.io Documentation highlighting between Sequential and Parallel Execution

In the previous article "Running Cypress Tests in GitHub Actions (Part 10)" of the Cypress-TestFramework article series, we saw how we can leverage Github to run our tests in a docker container within GitHub Actions CI/CD. In this #shorts article, we'll see about how to speed up the overall speed of Test Execution.

What is Test Execution Parallelization?

When we run our Tests one after another it takes a certain amount of time to complete those tests in a sequential manner. As a good practice in Test Automation:

A single test's execution shouldn't be dependent on the other tests' execution/s

Keeping this principle in mind we can ideally delegate running tests to different machines provided by CI/CD offering; hence considerably reducing the test execution time.

Here's a good article on Parallelization with Cypress:

Parallelization | Cypress Documentation
What you’ll learn How to parallelize test runs How to group test runs Strategies for grouping test runs How load-balancing of tests works What test

Cypress.io supports running parallel tests on a number of CI/CD providers. All we need is CI/CD provider configured to spin up multiple machines on demand.

All Cypress needs is passing --parallel flag along with the run command

cypress run --record --key=abc123 --parallel
Passing --parallel flag to a Cypress Command

That's about it.

Configuring GitHub Actions to support Parallelization:


You can directly clone this repo and use the following main.yml workflow file.

In our Workflow file in GitHub Actions, we just need to configure how many machines we need and how to pass --parallel flag to Cypress.

name: run-on-docker
on:
  # Triggers the workflow on push or pull request events but only for the develop branch
  push:
    branches: [ develop ]
jobs:
  cypress-run:
    runs-on: ubuntu-latest
    # Define Github strategy how to spin up new machines
    strategy:
     matrix:
      job: [0, 1, 2, 3]
    # Docker image with Cypress pre-installed
    # https://github.com/cypress-io/cypress-docker-images/tree/master/included
    container: cypress/included:9.2.0
    steps:
      - uses: actions/[email protected]
      - run: cd "Part 10" && npm install && npm run cy:record:parallel:qa --ci-build-id ${{ github.run_id }}
main.yml

this strategy matrix is the key here for spinning up new machines

strategy:
 matrix:
  job: [0, 1, 2, 3]

Here's a nice article on how to better leverage Github Action's strategy matrix:

How-to Github Actions: Build Matrix
How to use Build Matrix to test your project against different version of a language, a library or an operating system

Once done another thing I did is create a new script inside the package.json which has  a --parallel flag

...
"scripts": {
    ...
    "cy:record:parallel:qa": "node runner.js cypress run --env TAGS=\"@API\" configFile=qa --record --parallel --key YOUR_KEY_HERE --tag \"release,v1.10\""
...

Now once you commit these changes you should see CI/CD workflow in action.

Sample run:
https://github.com/far11ven/Cypress-TestFramework/actions/runs/2144385546

Tests will be spread across 4 jobs on different machines

Cypress-Dashboard result:

We can see 4 Specs present in the source repo were distributed among 3 different job/machines

Ciao!!

GitHub Repo:
https://github.com/far11ven/Cypress-TestFramework
Kushal Bhalaik

Kushal Bhalaik