Azure DevOps中运行C#单元测试的VsTest配置与依赖问题
I’ve run into this exact issue before—when your local Visual Studio tests run fine but the Azure DevOps pipeline throws that missing System.Runtime error, it almost always boils down to a .NET Core version mismatch between your development environment and the pipeline agent. Here’s how to fix it step by step:
1. Confirm your project’s .NET Core version first
First, check your test project’s .csproj file. Look for the <TargetFramework> (or <TargetFrameworks> if multi-targeting) element—this tells you which .NET Core version your tests depend on. For example:
<TargetFramework>netcoreapp3.1</TargetFramework>
2. Add the "Use .NET Core" task to your pipeline
You need to ensure the pipeline agent installs the exact same .NET Core SDK version your project uses. Add this task before your VsTest task in your YAML pipeline:
- task: UseDotNet@2 displayName: 'Install .NET Core SDK 3.1.x' # Replace with your project's version, e.g., 6.0.x, 7.0.x inputs: packageType: 'sdk' version: '3.1.x' # Match the major/minor version from your .csproj installationPath: $(Agent.ToolsDirectory)/dotnet
This task will fetch and install the correct SDK version on the agent, so your tests run in an environment matching your local setup.
3. Adjust your VsTest task configuration
Make sure your VsTest task is using the right context. Here’s a recommended configuration to pair with the SDK install task:
- task: VSTest@2 displayName: 'Run Unit Tests' inputs: testAssemblyVer2: | **\*test*.dll !**\*TestAdapter.dll !**\obj\** platform: '$(buildPlatform)' configuration: '$(buildConfiguration)' vsTestVersion: 'toolsInstaller' # Ensures we use the latest compatible test tools runInParallel: false # Optional, but can help with version-related issues
4. Additional checks if the issue persists
- If your project uses multiple target frameworks, add separate
UseDotNettasks for each version you need to test. - Verify that your build task (like
DotNetCoreCLI@2for build) is also using the same SDK version—add theUseDotNettask before the build step too, to ensure consistency across build and test stages. - If you have a
.runsettingsfile for your tests, make sure it’s referenced in the VsTest task’srunSettingsFileparameter, as it might specify framework-specific configurations.
This should resolve the missing System.Runtime assembly error by aligning the pipeline’s .NET Core environment with your local development setup.
内容的提问来源于stack exchange,提问作者BGCollector

