How to Run Cypress Tests: Simple Steps to Execute Your Tests
To run
Cypress tests, open your terminal and run npx cypress open to launch the interactive Test Runner or npx cypress run to execute tests in headless mode. The Test Runner lets you select and watch tests run in a browser, while the headless mode runs tests quickly in the background.Syntax
Use these commands in your project folder where Cypress is installed:
npx cypress open: Opens the Cypress Test Runner GUI to select and run tests interactively.npx cypress run: Runs all tests automatically in headless mode without opening a browser window.
You can add options like --spec to run specific test files.
bash
npx cypress open
npx cypress run
npx cypress run --spec "cypress/e2e/example.cy.js"Example
This example shows how to run Cypress tests using the Test Runner and headless mode.
First, run npx cypress open to open the Test Runner. Then click a test file to run it in the browser and see results live.
Alternatively, run npx cypress run to execute all tests in the terminal with a summary report.
bash
npx cypress open # After closing Test Runner, run all tests headlessly npx cypress run
Output
====================================================================================================
(Run Starting)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Cypress: 12.17.0 โ
โ Browser: Electron 112 (headless) โ
โ Specs: 1 found (example.cy.js) โ
โ Searched: cypress/e2e/example.cy.js โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Running: example.cy.js (1 of 1)
โ visits the example page
1 passing (1s)
====================================================================================================
(Run Finished)
Spec Tests Passes Failures Pending Skipped
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ example.cy.js 00:01 1 1 0 0 0 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โจ Done in 3 seconds.
Common Pitfalls
Common mistakes when running Cypress tests include:
- Running
npx cypress openwithout installing Cypress first. - Trying to run tests outside the project folder where Cypress is installed.
- Not specifying the correct test file path with
--specoption. - Confusing
npx cypress open(interactive) withnpx cypress run(headless).
Always ensure you have installed Cypress with npm install cypress --save-dev before running tests.
bash
Wrong: npx cypress run --spec "wrong/path/test.js" Right: npx cypress run --spec "cypress/e2e/test.js"
Quick Reference
| Command | Description |
|---|---|
| npx cypress open | Open Cypress Test Runner GUI to run tests interactively |
| npx cypress run | Run all tests headlessly in the terminal |
| npx cypress run --spec "path/to/test.js" | Run a specific test file |
| npm install cypress --save-dev | Install Cypress locally in your project |
Key Takeaways
Use
npx cypress open to run tests interactively with the Test Runner.Use
npx cypress run to run tests headlessly in the terminal.Always run commands inside your Cypress project folder where Cypress is installed.
Specify test files with
--spec option to run specific tests.Install Cypress first using
npm install cypress --save-dev before running tests.