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

如何修改cypress.dev.config.ts在Jenkins流水线中排除指定测试?

在Jenkins流水线中通过Cypress配置排除指定测试用例

可以通过修改cypress.dev.config.ts结合Jenkins环境变量实现需求,核心思路是根据运行环境(本地/CI)动态设置测试用例的排除规则,具体实现方式如下:

1. 配置环境变量驱动排除规则

在Jenkins流水线执行Cypress命令时,注入环境变量来指定需要排除的测试文件路径或标签:

  • 排除特定文件:设置EXCLUDE_TEST_PATTERNS变量,值为用逗号分隔的测试文件路径
  • 排除带特定标签的测试:设置CI变量标记当前为CI环境,后续在配置中过滤标签

2. 修改cypress.dev.config.ts配置

方式一:按文件路径排除

修改配置文件,读取环境变量并动态设置excludeSpecPattern:

import { defineConfig } from "cypress";

// 解析环境变量中的排除路径,默认空数组
const excludeSpecs = process.env.EXCLUDE_TEST_PATTERNS 
  ? process.env.EXCLUDE_TEST_PATTERNS.split(',') 
  : [];

export default defineConfig({
  defaultCommandTimeout: 60000,
  execTimeout: 60000,
  pageLoadTimeout: 120000,
  taskTimeout: 60000,
  requestTimeout: 120000,
  responseTimeout: 120000,
  viewportWidth: 1600,
  viewportHeight: 1100,
  e2e: {
    env: {
      loginUrl: '',
      loginOrigin: ''
    },
    // 动态添加排除规则
    excludeSpecPattern: [...excludeSpecs]
  },
  reporter: 'junit',
  reporterOptions: {
    mochaFile: 'results/cypress-report-[hash].xml',
    jenkinsMode: true
  }
});

方式二:按测试标签排除

先给不稳定的测试用例添加标签:

// 在测试文件中
describe('订单结算流程', { tags: '@flaky' }, () => {
  it('提交订单并支付', () => {
    // 测试逻辑
  });
});

然后修改配置文件,根据CI环境过滤标签:

import { defineConfig } from "cypress";

export default defineConfig({
  defaultCommandTimeout: 60000,
  execTimeout: 60000,
  pageLoadTimeout: 120000,
  taskTimeout: 60000,
  requestTimeout: 120000,
  responseTimeout: 120000,
  viewportWidth: 1600,
  viewportHeight: 1100,
  e2e: {
    env: {
      loginUrl: '',
      loginOrigin: '',
      // 默认不过滤标签,CI环境下排除@flaky标签
      TAGS: process.env.CI ? 'not @flaky' : ''
    },
    setupNodeEvents(on, config) {
      // 确保标签配置生效
      return config;
    }
  },
  reporter: 'junit',
  reporterOptions: {
    mochaFile: 'results/cypress-report-[hash].xml',
    jenkinsMode: true
  }
});

3. Jenkins流水线中执行命令

对应方式一(按路径排除)

在Jenkins的执行步骤中,设置环境变量并运行Cypress:

EXCLUDE_TEST_PATTERNS="cypress/e2e/flaky/order.cy.ts,cypress/e2e/flaky/payment.cy.ts" npx cypress run --config-file cypress.dev.config.ts

如果使用Jenkinsfile,可写为:

stage('Run Cypress Tests') {
  steps {
    sh '''
      export EXCLUDE_TEST_PATTERNS="cypress/e2e/flaky/order.cy.ts,cypress/e2e/flaky/payment.cy.ts"
      npx cypress run --config-file cypress.dev.config.ts
    '''
  }
}

对应方式二(按标签排除)

只需在Jenkins中设置CI=true环境变量即可:

CI=true npx cypress run --config-file cypress.dev.config.ts

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 19:43:39