GitHub Actions中Cypress JUnit Reporter仅生成最后一个测试套件XML报告问题
解决Cypress JUnit报告仅保留最后一个测试套件的问题
问题根源
JUnit Reporter 默认会覆盖同名输出文件,如果你的单元测试和端到端测试(或同一类型下的多个测试套件)输出到同一个XML文件,后运行的测试会完全覆盖之前的结果,最终只保留最后一个测试套件的内容。
解决方案
1. 为不同测试类型生成独立报告
给单元测试和E2E测试分别指定不同的输出文件,避免互相覆盖。修改GitHub Actions工作流代码如下:
- name: Generate Hash id: generate-hash run: | HASH=$(echo -n "$(date)" | sha256sum | cut -c1-8) echo "HASH_VALUE=$HASH" >> "$GITHUB_OUTPUT" - name: Run Unit Tests id: unit-test run: | npx nx run ${{ inputs.repo_project_name }}:test --reporter junit --reporter-options "mochaFile=${{ github.workspace }}/apps/${{ inputs.repo_project_name }}/cypress/unit-results/${{ steps.generate-hash.outputs.HASH_VALUE }}-unit-result.xml" - name: Run End-to-end Tests id: e2e-test run: | npx nx run ${{ inputs.repo_project_name }}:e2e --reporter junit --reporter-options "mochaFile=${{ github.workspace }}/apps/${{ inputs.repo_project_name }}/cypress/e2e-results/${{ steps.generate-hash.outputs.HASH_VALUE }}-e2e-result.xml"
2. 合并多个报告(可选,生成统一完整报告)
如果需要将单元和E2E测试结果合并成一个XML文件,可使用junit-report-merger工具:
# 在测试完成后添加以下步骤 - name: Install JUnit Report Merger run: npm install -g junit-report-merger - name: Merge Test Reports run: | mkdir -p ${{ github.workspace }}/combined-results jrm "${{ github.workspace }}/combined-results/${{ steps.generate-hash.outputs.HASH_VALUE }}-combined-results.xml" \ "${{ github.workspace }}/apps/${{ inputs.repo_project_name }}/cypress/unit-results/*.xml" \ "${{ github.workspace }}/apps/${{ inputs.repo_project_name }}/cypress/e2e-results/*.xml"
3. 单测试类型内避免套件覆盖(额外优化)
如果同一测试类型(比如E2E)下有多个套件也出现覆盖问题,可给mochaFile配置添加通配符,让每个套件生成独立文件后再合并:
- name: Run End-to-end Tests id: e2e-test run: | npx nx run ${{ inputs.repo_project_name }}:e2e --reporter junit --reporter-options "mochaFile=${{ github.workspace }}/apps/${{ inputs.repo_project_name }}/cypress/e2e-results/${{ steps.generate-hash.outputs.HASH_VALUE }}-e2e-*.xml"
之后用同样的合并命令将这些分散的XML文件合并成一个完整报告。
内容的提问来源于stack exchange,提问作者Shivam Srivastava
相关产品推荐
相关产品推荐

