You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress测试执行控制:多文件夹测试选择更优方案咨询

Better Ways to Run Cypress Tests by Folder (or Groups)

Great question—you’re definitely not alone in wanting more flexible test folder selection for Cypress! As test suites grow, relying on manual terminal commands or moving folders around gets tedious fast. Here are several cleaner, more scalable approaches to handle your scenarios:

1. Simplify Commands with npm Scripts & Multi-Path Spec Patterns

Cypress’s --spec flag supports multiple glob paths, so you can run tests from multiple folders in a single command. Pair this with npm scripts to avoid typing long commands every time.

For Scenario 1 (Run a single feature folder):

Add this to your package.json:

"scripts": {
  "test:feature2": "cypress run --spec 'cypress/integration/testing-feature-2/**/*' --browser canary --no-exit"
}

Run it with:

npm run test:feature2

For Scenario 2 (Run multiple feature folders):

Update your script to include multiple paths:

"scripts": {
  "test:feature2+3": "cypress run --spec 'cypress/integration/testing-feature-2/**/*','cypress/integration/testing-feature-3/**/*' --browser canary --no-exit"
}

Run with:

npm run test:feature2+3

For Scenario 3 (Run all except seeding):

Use the --ignore flag (available in Cypress 10+) to exclude the seeding folder:

"scripts": {
  "test:all-except-seeding": "cypress run --ignore 'cypress/integration/seeding/**/*' --browser canary --no-exit"
}

Run with:

npm run test:all-except-seeding

2. Dynamic Test Groups via Cypress Config

If you want even more flexibility, define test groups in your cypress.config.js file. This lets you switch between groups using environment variables, which is great for CI or repeated runs.

Here’s an example config:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    specPattern: 'cypress/integration/**/*',
    setupNodeEvents(on, config) {
      // Use an environment variable to select the test group
      switch(config.env.testGroup) {
        case 'feature2':
          config.specPattern = 'cypress/integration/testing-feature-2/**/*';
          break;
        case 'feature2+3':
          config.specPattern = [
            'cypress/integration/testing-feature-2/**/*',
            'cypress/integration/testing-feature-3/**/*'
          ];
          break;
        case 'all-except-seeding':
          config.ignoreTestFiles = 'cypress/integration/seeding/**/*';
          break;
        // Default to all tests if no group is specified
        default:
          config.specPattern = 'cypress/integration/**/*';
      }
      return config;
    }
  }
})

Run a group with:

npx cypress run --env testGroup=feature2 --browser canary --no-exit

3. Use the Cypress UI for Interactive Folder Selection

If you prefer a graphical interface, the Cypress UI (npx cypress open) lets you run entire folders or select multiple folders at once:

  • Single folder: Open the UI, find your testing-feature-2 folder, and click the play button next to its name.
  • Multiple folders: Hold Ctrl (Windows) or Cmd (Mac) to select multiple folders, then click the "Run X specs" button at the top to run all their tests.
  • Exclude folders: If you need to skip seeding, use the search bar to filter it out, then select all remaining specs and run them.

This is perfect for ad-hoc testing where you want to quickly pick and choose folders without editing scripts.

Scaling for Future testing-feature-x Folders

All these approaches scale easily as you add new feature folders:

  • For npm scripts, just add a new script line with the new folder path.
  • For the config file, add a new case to the switch statement.
  • For the UI, new folders will automatically show up in the test list.

No more messy folder moves or repetitive terminal commands!

内容的提问来源于stack exchange,提问作者Zeth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 09:13:12