GitHub Actions中dotnet test未执行测试的问题求助
解决GitHub Actions中.NET测试未实际执行的问题
问题场景
我正在创建GitHub Actions工作流构建并测试.NET项目,工作流文件如下:
name: build and test on: push: pull_request: branches: [ main ] paths: - '**.cs' - '**.csproj' env: DOTNET_VERSION: '7.0' # The .NET SDK version to use jobs: build-and-test: name: build-and-test-${{matrix.os}} runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest] steps: - uses: actions/checkout@v3 - name: Setup .NET Core uses: actions/setup-dotnet@v3 with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Install dependencies run: dotnet restore Starmap/Starmap.sln - name: Build run: dotnet build Starmap/Starmap.sln --configuration Release --no-restore - uses: actions/checkout@v3 - uses: isbang/compose-action@v1.4.1 with: compose-file: "infra.yaml" - name: Test run: dotnet test Starmap/Starmap.sln --no-restore --verbosity normal
所有步骤显示成功,但测试并未实际运行(我特意修改测试用例使其失败,却未触发工作流失败),且dotnet test步骤输出仅包含构建成功信息,无测试执行记录:
Run dotnet test Starmap/Starmap.sln --no-restore --verbosity normal dotnet test Starmap/Starmap.sln --no-restore --verbosity normal shell: /usr/bin/bash -e {0} env: DOTNET_VERSION: 7.0 DOTNET_ROOT: /usr/share/dotnet Build started 02/18/2023 11:41:46. 1>Project "/home/runner/work/Starmap/Starmap/Starmap/Starmap.sln" on node 1 (VSTest target(s)). 1>ValidateSolutionConfiguration: Building solution configuration "Debug|Any CPU". 1>Done Building Project "/home/runner/work/Starmap/Starmap/Starmap/Starmap.sln" (VSTest target(s)). Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:01.67
问题原因
- 重复执行
actions/checkout@v3:第一次checkout已经拉取代码并完成依赖安装、构建,第二次checkout会将工作目录重置为仓库原始状态,覆盖了特意修改的测试用例,同时丢失之前的构建产物。 - 测试与构建配置不一致:
dotnet build使用Release配置,但dotnet test默认用Debug配置,加上--no-restore参数后,测试步骤会重新构建Debug版本,却未正确触发测试执行。 - 基础设施服务未就绪:使用
compose-action启动服务后直接执行测试,可能因依赖服务未启动导致测试静默跳过。
修复方案
1. 移除重复的checkout步骤
删除第二次的- uses: actions/checkout@v3,保留第一次即可,避免代码和构建产物被重置。
2. 统一测试与构建配置
在dotnet test命令中添加--configuration Release参数,与构建阶段保持一致,避免重复构建:
dotnet test Starmap/Starmap.sln --configuration Release --no-restore --verbosity normal
3. 等待基础设施服务就绪
在测试前添加步骤,等待服务启动完成(示例以数据库端口5432为例,需根据实际服务调整):
- name: Wait for services to be ready run: | until nc -z localhost 5432; do sleep 1 done
修复后的完整工作流
name: build and test on: push: pull_request: branches: [ main ] paths: - '**.cs' - '**.csproj' env: DOTNET_VERSION: '7.0' # The .NET SDK version to use jobs: build-and-test: name: build-and-test-${{matrix.os}} runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest] steps: - uses: actions/checkout@v3 - name: Setup .NET Core uses: actions/setup-dotnet@v3 with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Install dependencies run: dotnet restore Starmap/Starmap.sln - name: Build run: dotnet build Starmap/Starmap.sln --configuration Release --no-restore - uses: isbang/compose-action@v1.4.1 with: compose-file: "infra.yaml" - name: Wait for services to be ready run: | # 根据你的基础设施服务调整端口和等待逻辑 until nc -z localhost 5432; do sleep 1 done - name: Test run: dotnet test Starmap/Starmap.sln --configuration Release --no-restore --verbosity normal
额外检查点
- 确认解决方案文件
Starmap.sln已正确包含所有测试项目,且测试项目的TargetFramework与.NET版本匹配。 - 若测试需要特定环境变量,可在
env字段或测试步骤中添加。
内容的提问来源于stack exchange,提问作者Eliran Turgeman
相关产品推荐
相关产品推荐

