Yesterday I have shown how to run Cypress specs in parallel for free using some of the most popular CI providers: GitHub Actions, CircleCI, and GitLab CI. Today, I will show you how to store test artifacts to help you understand the test failures on CI. We will generate Mochawesome HTML reports, and I will show a single CI job and a parallel one using cypress-split.
The test artifacts
I will start this blog post with the results achieved. On every commit, GitHub Actions test my application using cypress run
command. Cypress saves screenshots and videos plus I have configured Mochawesome reporter to save the HTML reports. I have introduced a failed test on purpose to make the test artifacts useful:
🎁 You can find the full source code and the test artifacts in the repo bahmutov/cy-report-example.
Under the workflow summary, you can see all test artifacts. We have a single "standard" job with all specs, plus 3 parallel test jobs running the same specs.
Let's download the artifact "standard-cypress-screenshots" to see how our Test Runner looked when it failed:
We can see that the spec viewports.cy.ts
has a failure. Let's look at the Mochawesome HTML report in the test artifact "standard-mochawesome". When we download the Zip archive from GitHub, we get a folder with a separate HTML report for each spec
1 | standard-mochawesome/ |
Let's open the viewports.html
report.
In the failed test, we have its source code plus the error message. Taken together with the Test Runner screenshot and the "standard-cypress-videos" artifact with the run MP4 videos, we have enough information to determine the cause of the failure.
Cypress configuration
The Cypress configuration is very straightforward. We will add the Mochawesome plugins (plus cypress-split
if you want)
1 | $ npm i -D mochawesome mochawesome-merge mochawesome-report-generator |
In the cypress.config.ts
add the reporter settings
1 | import { defineConfig } from 'cypress' |
When you use cypress run
command you should see messages after each spec file.
All reports are saved in cypress/results
folder. You can open them locally - these are just static files.
GitHub Actions workflow
Let's configure the workflow. I will use my cypress-workflows reusable workflows to run Cypress tests.
1 | name: ci |
The workflow standard.yml has a parameter store-artifacts
that is on by default:
1 | store-artifacts: |
If you look at the output of the workflow when it runs, you will see how it stores cypress/screenshots
, cypress/videos
, and cypress/results
folders (if they exist).
Split GitHub Actions workflow
Let's speed the tests by running them in parallel. I have made split.yml reusable workflow and it includes the parameter store-artifacts=true
by default. Thus our CI workflow file .github/workflows/ci.yml is a one-liner still:
1 | name: ci |
This workflow adds an index to each saved artifact to avoid overwriting them. In the workflow summary, we see the failed viewport.cy.ts
spec was included in the 3rd CI job.
The 3rd machine means its index is 2, and we should inspect the artifacts "split-2-..." to see the test results.
Nice.
Future work
Currently we have generated a separate Mochawesome HTML report for each spec file. In the next installment, I will combine the separate reports into a single report with all tests.
See also
- The next blog post The Battle of Cypress Mochawesome Reporters that continues where this blog ends.
- Cypress reporters guide
- Integrating Mochawesome reporter with Cypress