GitHub Actions复用.NET构建产物运行集成测试的路径与参数问题
初始问题
构建工作流片段
- name: Upload build artifacts uses: actions/upload-artifact@v4.3.1 with: name: dll-artifacts path: '**/bin/Release/**/*.dll'
测试工作流片段
- name: Download build artifacts uses: actions/download-artifact@v4.1.4 with: name: dll-artifacts path: build - name: Run Integration Tests run: dotnet test --configuration Release --no-build ~/build/MyProject.IntegrationTests.dll
出现的错误
The argument /home/runner/build/MyProject.IntegrationTests.dll is invalid. Please use the /help option to check the list of valid arguments.
同时提示:The following arguments have been ignored : "--configuration Release --no-build"
更新后的问题
修复路径后,运行测试DLL时又出现新错误:
Testhost process for source(s) exited with error: A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found. Failed to run as a self-contained app. - The application was run as a self-contained app because 'testhost.runtimeconfig.json' was not found.
解决步骤
1. 修正dotnet test的参数与路径
dotnet test要求全局参数放在目标文件之前,且GitHub Runner中不建议用~/简写路径,直接用相对路径或工作目录变量更可靠。
修正后的测试命令:
dotnet test --configuration Release --no-build ./build/MyProject.IntegrationTests.dll
用工作目录变量确保路径绝对准确:
dotnet test --configuration Release --no-build ${{ github.workspace }}/build/MyProject.IntegrationTests.dll
2. 补充上传测试必需的依赖文件
之前只上传了DLL,导致测试运行时缺少配置文件和依赖库。修改构建工作流的上传步骤,包含所有必要文件:
方式一:明确列出必要文件类型
- name: Upload build artifacts uses: actions/upload-artifact@v4.3.1 with: name: test-artifacts path: | **/bin/Release/**/*.dll **/bin/Release/**/*.json **/bin/Release/**/*.so **/bin/Release/**/*.deps.json **/bin/Release/**/*.runtimeconfig.json
方式二:直接上传整个Release目录(更简单)
如果不需要过滤文件,直接上传整个Release输出目录:
- name: Upload build artifacts uses: actions/upload-artifact@v4.3.1 with: name: test-artifacts path: '**/bin/Release/'
3. 匹配实际目录结构调整测试路径
下载产物后的路径结构和上传时一致,需根据实际情况调整测试命令路径。比如测试DLL原本在bin/Release/net8.0/MyProject.IntegrationTests.dll,下载后路径为build/net8.0/MyProject.IntegrationTests.dll,对应的测试命令为:
dotnet test --configuration Release --no-build ./build/net8.0/MyProject.IntegrationTests.dll
内容的提问来源于stack exchange,提问作者freshbm

