Learn a simple technique to perform automated tests in different environments, overwriting the Cypress configuration
Resuming the series "Pinches of Cypress", I decided to bring a simple but recurring theme in discussion forums about testing automation.
Imagine that you need to run the same set of end-to-end tests against a local development environment, in a staging environment, and then in production.
Let's say the URLs for the environments are as follows:
And in the Cypress settings file, the baseUrl
property has the value http://localhost:8000
. That is, it points to the local development environment.
In the package.json
file, we could have the following scripts:
"scripts": {
"test": "cypress run",
"test:staging": "cypress run --config baseUrl=https://example.com/my-app-staging",
"test:prod": "cypress run --config baseUrl=https://example.com/my-app"
}
Finally, at the terminal, or even in a continuous integration pipeline, execute the following commands to run the tests against the different environments.
-
npm test
(ornpm t
- short version) - to run tests against the local development environment -
npm run test:staging
- to run the tests against the staging environment - and
npm run test:prod
- to run the tests in production
That's it!
And just as baseUrl
can be overwritten, so can other configurations.
Tell me, what else would you like to see in the series "Pinches of Cypress"?
This post was originally published in Portuguese on the Talking About Testing blog.
Would you like to learn about test automation with Cypress? I was hoping you could get to know my online courses on Udemy. Happy testing! 🎉