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

GitLab CI配置问题:两个.NET服务仅一个生成XML报告及制品

GitLab CI中.NET服务测试报告生成失败问题修复

问题背景

我需要在GitLab中为两个.NET服务配置流水线测试结果,目前仅my-service-2-tests可正常生成XML报告与制品,my-service-1-tests无法生成对应文件。

当前gitlab-ci.yml配置

my-service-1-tests:
  stage: test
  script:
    - dotnet test $SRC_DIRECTORY/$TESTS_DIRECTORY/$MY_SERVICE_1_TESTS --logger:"junit;LogFilePath=..\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
  allow_failure: true 
  artifacts:
    when: always
    paths:
      - ./**/*test-result.xml
    reports:
      junit:
        - ./**/*test-result.xml
        
my-service-2-tests:
  stage: test
  script:
    - dotnet test $SRC_DIRECTORY/$TESTS_DIRECTORY/$MY_SERVICE_2_TESTS --logger:"junit;LogFilePath=..\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
  allow_failure: true 
  artifacts:
    when: always
    paths:
      - ./**/*test-result.xml
    reports:
      junit:
        - ./**/*test-result.xml

my-service-1-tests执行错误日志

Failed!  - Failed:     2, Passed:    13, Skipped:     0, Total:    15, Duration: 3 s - /builds/my-app/backend/my-service-1/src/tests/MyService1.Tests/bin/Debug/net6.0/MyService1.Tests.dll (net6.0)
Uploading artifacts for failed job
00:01
Uploading artifacts...
WARNING: src/tests/artifacts/*-test-result.xml: no matching files. Ensure that the artifact path is relative to the working directory (/builds/my-app/backend/my-service-1) 
ERROR: No files to upload                          
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

已尝试使用绝对路径、手动创建artifact文件夹等方法,问题仍存在。另外发现两个并行执行的job中,my-service-1-tests的Git仓库被重新初始化,my-service-2-tests创建的是空仓库,怀疑此为问题根源,需修复gitlab-ci.yml配置让两个服务都能生成对应制品。


修复方案

1. 统一路径分隔符,适配Linux Runner环境

GitLab Runner默认使用Linux环境,原配置中用了Windows风格的\分隔符,替换为Linux的/:
修改dotnet test命令中的LogFilePath参数:

--logger:"junit;LogFilePath=../artifacts/{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"

2. 明确指定工作目录,避免路径混乱

为每个测试job设置working_directory,确保执行命令的路径正确,同时规避仓库初始化异常:

my-service-1-tests:
  stage: test
  working_directory: $CI_PROJECT_DIR/src/tests/MyService1.Tests
  script:
    - dotnet test --logger:"junit;LogFilePath=../../artifacts/{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
  allow_failure: true 
  artifacts:
    when: always
    paths:
      - $CI_PROJECT_DIR/artifacts/*test-result.xml
    reports:
      junit:
        - $CI_PROJECT_DIR/artifacts/*test-result.xml

my-service-2-tests:
  stage: test
  working_directory: $CI_PROJECT_DIR/src/tests/MyService2.Tests
  script:
    - dotnet test --logger:"junit;LogFilePath=../../artifacts/{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
  allow_failure: true 
  artifacts:
    when: always
    paths:
      - $CI_PROJECT_DIR/artifacts/*test-result.xml
    reports:
      junit:
        - $CI_PROJECT_DIR/artifacts/*test-result.xml

3. 提前创建artifacts目录,确保路径存在

在script步骤中先创建目标目录,避免报告生成时路径不存在:

script:
  - mkdir -p $CI_PROJECT_DIR/artifacts
  - dotnet test --logger:"junit;LogFilePath=$CI_PROJECT_DIR/artifacts/{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"

4. 控制Git仓库初始化策略

如果是多仓库/子模块导致的重复初始化问题,添加变量控制Git行为:

my-service-1-tests:
  variables:
    GIT_STRATEGY: fetch
    GIT_CLEAN_FLAGS: -ffdx -e artifacts/

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 12:27:56