.NET Core项目改为.NET Framework 4.7后无法运行VS测试
Hey there, I’ve run into this exact scenario before—when you switch your main project from .NET Core to .NET Framework, the test projects need to play by the same rules to work properly. Let’s break down the fixes step by step:
Match the target framework in your test project
The biggest mistake here is keeping the test project targeted at .NET Core while your main project is on net47. Either create a new test project specifically for .NET Framework (look for options like "NUnit Test Project (.NET Framework)" in the Visual Studio template selector), or edit your existing test project’s.csprojfile to set:<TargetFramework>net47</TargetFramework>Make sure this matches the target framework of your API project exactly.
Verify NuGet packages for testing frameworks
For NUnit, you’ll need these packages installed in your test project (compatible with net47):NUnit(v3.x works great with net47)NUnit3TestAdapter(this lets Visual Studio detect and run NUnit tests)
If you were using xUnit or MS Test, install their respective .NET Framework-compatible packages and adapters too—skip the .NET Core-specific ones.
Clean up and rebuild your solution
Sometimes stale build artifacts cause the test runner to hang. Go toBuild > Clean Solution, then delete thebinandobjfolders from both your API and test projects manually. Rebuild the entire solution afterwards.Check Visual Studio’s test runner settings
Head toTest > Settings > Test Settingsand make sure you haven’t accidentally excluded .NET Framework tests. Also, if you’re on an older version of Visual Studio, update to the latest stable release—some older versions have bugs with running .NET Framework tests alongside former .NET Core projects.Test via command line to rule out IDE issues
If clicking "Run Tests" still does nothing, try running tests from the command line. For NUnit, use:dotnet testOr use the Visual Studio test console:
vstest.console.exe YourTestProject.dllThis will spit out error messages if there’s an underlying configuration problem that the IDE isn’t showing you.
Double-check project references
Ensure your test project has a valid reference to your API project. Sometimes when switching frameworks, the reference can break—remove and re-add it if needed, and confirm the API project builds successfully on its own.
内容的提问来源于stack exchange,提问作者Alex 75

