GitHub Action中dotnet test无法找到.NET 8框架,但构建正常
问题:GitHub Action中
dotnet test执行失败提示缺少框架 构建步骤可正常运行,但dotnet test步骤报错:
.NET location: /usr/share/dotnet/ No frameworks were found.
已尝试以下操作但未解决:
- 确认
/usr/share/dotnet/目录存在 - 显式设置环境变量
DOTNET_ROOT: /usr/share/dotnet/ - 在测试步骤前重新执行
dotnet restore
测试项目与应用项目的版本配置一致:
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework> <EnableWindowsTargeting>true</EnableWindowsTargeting> <SupportedOSPlatformVersion>10.0.17763.0</SupportedOSPlatformVersion>
完整工作流配置:
name: Build and Test workflow on: [push, workflow_dispatch] jobs: build: runs-on: ubuntu-latest strategy: matrix: dotnet-version: [ '8.x' ] steps: - uses: actions/checkout@v4 - name: Setup dotnet ${{ matrix.dotnet-version }} uses: actions/setup-dotnet@v3 with: dotnet-version: ${{ matrix.dotnet-version }} - name: Display dotnet version run: dotnet --version - name: Install dependencies run: dotnet restore - name: Build run: dotnet build - name: Show runtime just to check it is there run: | ls /usr/share/dotnet/ - name: Test with dotnet, simplified # env: # DOTNET_ROOT: /usr/share/dotnet/ run: dotnet test - name: Test with dotnet, the real thing I wish to run run: dotnet test --logger trx --results-directory "TestResults-${{ matrix.dotnet-version }}" - name: Upload dotnet test results uses: actions/upload-artifact@v3 with: name: dotnet-results-${{ matrix.dotnet-version }} path: TestResults-${{ matrix.dotnet-version }} condition: always() # this step will always run, even if the pipeline is cancel
日志显示.NET 8.0.101已成功安装,但测试时提示:
2024-01-16T20:33:41.5843829Z Testhost process for source(s) '/home/runner/work/Sams/Sams/SamsUnitTests/bin/Debug/net8.0-windows10.0.17763.0/SamsUnitTests.dll' exited with error: You must install or update .NET to run this application. 2024-01-16T20:33:41.5857865Z Framework: 'Microsoft.WindowsDesktop.App', version '8.0.0' (x64) 2024-01-16T20:33:41.5859546Z No frameworks were found.
原因分析
- 测试项目的目标框架
net8.0-windows10.0.17763.0是Windows专属的.NET桌面框架,依赖Microsoft.WindowsDesktop.App运行时。 - 工作流使用的
ubuntu-latest是Linux环境,Linux系统默认不会安装Windows桌面运行时;即便安装了.NET 8 SDK,也没有对应的Windows桌面运行时支持,因此测试执行时找不到所需框架。 - 构建步骤能成功是因为.NET SDK支持跨平台构建(可在Linux上编译Windows目标项目),但测试步骤需要实际运行项目,而Linux无法运行Windows专属的桌面应用或测试项目。
解决方向
- 将工作流的运行环境改为
windows-latest,与项目的Windows目标框架匹配 - 若需在Linux上测试,需调整项目的目标框架为跨平台的
net8.0(移除Windows特定版本后缀),同时确保测试项目不依赖Windows专属API
内容的提问来源于stack exchange,提问作者Horst Walter
相关产品推荐
相关产品推荐

