#atom
Title: Running Cypress Tests - npx cypress run
and Variants
Tags: #cypress #testing #cli #npx #e2e
Overview:
Cypress provides a powerful command-line interface (CLI) for running tests. The primary command for executing tests in headless mode is npx cypress run
. This command is essential for integrating Cypress into CI/CD pipelines and automating test execution. Cypress also offers several variants of this command to customize test runs.
Basic Command:
npx cypress run
- Runs all Cypress tests in headless mode (no browser UI).
- Outputs results to the terminal.
- Default browser: Electron.
Key Variants of npx cypress run
:
-
Specify Browser:
Run tests in a specific browser (e.g., Chrome, Firefox).npx cypress run --browser chrome
-
Run Specific Test File:
Execute a single test file.npx cypress run --spec "cypress/e2e/login.spec.js"
-
Run Tests in Headed Mode:
Open the browser UI for debugging.npx cypress run --headed
-
Run Tests in Parallel:
Distribute tests across multiple machines for faster execution.npx cypress run --parallel
-
Record Test Results:
Record test results to the Cypress Dashboard (requires a project ID).npx cypress run --record --key <record-key>
-
Specify Environment Variables:
Pass environment variables for dynamic configuration.npx cypress run --env apiUrl=https://example.com
-
Run Tests in a Specific Group:
Group tests for better organization in the Dashboard.npx cypress run --group "login-tests"
-
Retry Failed Tests:
Automatically retry failed tests a specified number of times.npx cypress run --retries 2
Common Use Cases:
- CI/CD Integration: Use
npx cypress run
in CI pipelines to automate testing. - Debugging: Use
--headed
mode to visually debug failing tests. - Parallel Execution: Speed up test suites by running tests in parallel across multiple machines.
- Reporting: Record test results to the Cypress Dashboard for analytics and insights.
Example Workflow:
- Run all tests in Chrome:
npx cypress run --browser chrome
- Run a specific test file in headed mode:
npx cypress run --spec "cypress/e2e/login.spec.js" --headed
- Record test results to the Dashboard:
npx cypress run --record --key <record-key>
References:
Connections:
Sources:
- From: Cypress