如何在Cypress/Cucumber/TS+Jenkins环境配置待执行用例的Allure报告
问题描述
我在用Cypress+Cucumber+TypeScript做项目,只有通过/失败的测试用例时,Allure报告能正常生成;但用Cucumber标签生成待执行(pending)用例时,报告生成失败——显示NaN,而且allure-results文件夹都不存在。测试在Jenkins/Docker环境跑,试过always、changed、fixed等报告生成策略都没用。
可能原因及修复方案
1. Pending用例未触发Allure结果生成
Cucumber的pending用例默认不会被Cypress的Allure插件捕获,导致没有生成任何测试结果文件,allure-results文件夹自然不会创建,Jenkins的Allure插件找不到结果就会显示NaN。
修复步骤:
- 确保
cypress-allure-plugin版本升级到v2.12.0以上,新版本对Cucumber的pending/skipped用例支持更完善 - 在
cypress.config.ts中配置Allure插件,开启skipped/pending用例追踪:
import { defineConfig } from 'cypress'; import allureWriter from '@shelex/cypress-allure-plugin/writer'; export default defineConfig({ e2e: { setupNodeEvents(on, config) { allureWriter(on, config, { skippedTestStatus: 'pending' // 将skipped用例标记为pending,确保Allure捕获 }); return config; }, specPattern: 'cypress/e2e/**/*.feature' } });
2. 新增目录兜底逻辑
如果全是pending用例,allure-results目录不会自动创建,Jenkins的Allure插件会报错。需要在生成报告前强制创建目录:
修改流水线post阶段的脚本:
post { always { script { // 确保目录存在,避免无测试结果时的报错 sh 'mkdir -p ui-auto-tests/allure-results' allure([ includeProperties: false, jdk: '', properties: [], reportBuildPolicy: 'ALWAYS', results: [[path: 'ui-auto-tests/allure-results']] ]) } } // 原slackSend逻辑保持不变 }
3. 修复Cypress运行权限问题
Docker容器中可能存在目录创建权限不足的情况,在Cypress运行前先创建并授权allure-results目录:
修改Cypress阶段的执行脚本:
sh ''' mkdir -p ui-auto-tests/allure-results && chmod -R 777 ui-auto-tests/allure-results NO_COLOR=1 \\ CYPRESS_CACHE_FOLDER=/tmp/.cypress-cache \\ npx cypress run --browser chrome --env allure=true '''
4. 确保Cucumber标签生效
如果是通过标签筛选pending用例,需要在运行命令中明确指定标签参数:
npx cypress run --browser chrome --env allure=true,tags="@pending"
原始流水线脚本
pipeline { agent { kubernetes { yaml """ apiVersion: v1 kind: Pod spec: containers: - name: cypress image: cypress/included:12.10.0 command: - cat tty: true - name: git image: alpine/git command: - cat tty: true securityContext: runAsUser: 1000 runAsGroup: 3000 fsGroup: 2000 """ } } options { timeout(time: 60, unit: 'MINUTES') buildDiscarder(logRotator(numToKeepStr: '10')) } stages { stage('Clone repository') { steps { container('git') { git( url: 'tests/explorer.git', credentialsId: 'tests-explorer', branch: 'dev' ) } } } stage('Cypress') { steps { container('cypress') { dir('ui-auto-tests') { sh ''' CYPRESS_CACHE_FOLDER=/tmp/.cypress-cache \\ npm install ./node_modules/.bin/cypress install --force ''' sh ''' NO_COLOR=1 \\ CYPRESS_CACHE_FOLDER=/tmp/.cypress-cache \\ npx cypress run --browser chrome --env allure=true ''' } } } } } post { always { script { allure([ includeProperties: false, jdk: '', properties: [], reportBuildPolicy: 'ALWAYS', results: [[path: 'ui-auto-tests/allure-results']] ]) } } success { slackSend channel: 'reporting-qa', color: '#00FF00', message: "${env.JOB_NAME}\n Build ID: ${env.BUILD_ID}\n Status: Success\n Duration: ${currentBuild.durationString}\n Allure: (<${env.BUILD_URL}/allure|open>)" } failure { slackSend channel: 'reporting-qa', color: 'FF0000', message: "${env.JOB_NAME}\n Build ID: ${env.BUILD_ID}\n Status: Failed\n Duration: ${currentBuild.durationString}\n Allure: (<${env.BUILD_URL}/allure|open>)" } aborted { slackSend channel: 'reporting-qa', color: '808080', message: "${env.JOB_NAME}\n Build ID: ${env.BUILD_ID}\n Status: Pending\n Duration: ${currentBuild.durationString}\n Allure: (<${env.BUILD_URL}/allure|open>)" } } }
内容的提问来源于stack exchange,提问作者Dev Perera
相关产品推荐
相关产品推荐

