#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

Key Variants of npx cypress run:

  1. Specify Browser:
    Run tests in a specific browser (e.g., Chrome, Firefox).

    npx cypress run --browser chrome
    
  2. Run Specific Test File:
    Execute a single test file.

    npx cypress run --spec "cypress/e2e/login.spec.js"
    
  3. Run Tests in Headed Mode:
    Open the browser UI for debugging.

    npx cypress run --headed
    
  4. Run Tests in Parallel:
    Distribute tests across multiple machines for faster execution.

    npx cypress run --parallel
    
  5. Record Test Results:
    Record test results to the Cypress Dashboard (requires a project ID).

    npx cypress run --record --key <record-key>
    
  6. Specify Environment Variables:
    Pass environment variables for dynamic configuration.

    npx cypress run --env apiUrl=https://example.com
    
  7. Run Tests in a Specific Group:
    Group tests for better organization in the Dashboard.

    npx cypress run --group "login-tests"
    
  8. Retry Failed Tests:
    Automatically retry failed tests a specified number of times.

    npx cypress run --retries 2
    

Common Use Cases:


Example Workflow:

  1. Run all tests in Chrome:
    npx cypress run --browser chrome
    
  2. Run a specific test file in headed mode:
    npx cypress run --spec "cypress/e2e/login.spec.js" --headed
    
  3. Record test results to the Dashboard:
    npx cypress run --record --key <record-key>
    

References:


Connections:


Sources: