使用NUnit3控制台运行测试时遇System.Runtime 4.2.0.0未找到异常求助
Hey there, I’ve run into similar headaches working with NUnit and .NET Core projects, so let’s break down what’s going on and how to fix it:
Why This Happens
The core issue here is a compatibility mismatch between the NUnit Console Runner version you’re using (3.8.0) and your .NET Core 2.0 test project. The traditional nunit3-console.exe is built for .NET Framework, and while it supports .NET Core, older versions have limited compatibility with newer .NET Core runtime assemblies like System.Runtime 4.2.0.0. Your .NET Framework 4.5 test works fine because the console runner is natively designed for that environment.
Step-by-Step Solutions
1. Upgrade the NUnit Console Runner to Match Your Framework Version
You mentioned installing NUnit framework 3.10.1, but you’re still using the outdated 3.8.0 console runner. Make sure they’re in sync:
- Uninstall the old console runner package:
Uninstall-Package nunit.consolerunner -Version 3.8.0 - Install the compatible version (3.10.1 or newer):
Then retry running your tests with the updated runner.Install-Package nunit.consolerunner -Version 3.10.1
2. Use dotnet test Instead of nunit3-console.exe (Recommended for .NET Core)
For .NET Core projects, the most reliable way to run tests is using the dotnet test command—it automatically handles .NET Core runtime dependencies without manual configuration. Here’s how:
- Navigate to your test project directory in the console:
cd D:\Code_Trials\BoMLibrary\BomTest - Run the tests:
Ensure your test project has these essential NuGet packages installed (add them if missing):dotnet testMicrosoft.NET.Test.SdkNUnit3TestAdapter
3. Verify Your Test Project’s .csproj Configuration
Double-check your .csproj file to confirm it’s properly set up for .NET Core 2.0 and includes all required packages. Here’s a minimal working example:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include="NUnit" Version="3.10.1" /> <PackageReference Include="NUnit3TestAdapter" Version="3.10.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> </ItemGroup> </Project>
4. Skip Manual System.Runtime Installs
You don’t need to install System.Runtime manually via NuGet for .NET Core projects—it’s included as part of the .NET Core runtime. Manually adding specific versions will only cause conflicts, so remove any System.Runtime packages you installed earlier.
Additional Checks
If you’re still stuck, confirm your machine has the .NET Core 2.0 SDK/runtime installed. You can verify this with:
dotnet --list-runtimes
Missing runtime components often lead to assembly loading errors like the one you’re seeing.
内容的提问来源于stack exchange,提问作者Rahul

