How To Resolve Issue Of Test Project Not Running The Unit Tests After Upgrade To .NET 6

Introduction

I was upgrading the .NET Core 3.1 project to .NET 6 and faced issues running test cases in my test projects. I had several projects and test projects for each project; all were working fine after the upgrade. However, I got an issue with only two test projects after the upgrade. So here in this article, I am going to share the issue and the solution to how I fixed it.

Even though there was no single error after the upgrade, the solution was cleaned and built successfully I was not able to run unit tests of two projects.

Once, the upgrade is completed I was testing the solution where I got an error: “unexpected error detected. Check the Tests output Pane for details.”

Two of my test projects didn’t run the test cases, it was about to start and took some time to run the test but got stopped after a few minutes. This is the scenario of the Test project using xunit framework for unit tests.

The below image shows the error that I got while testing.

When I checked the Output pane the exact error was

“Starting test discovery for requested test run

========== Starting test discovery ==========

Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Failed to negotiate protocol, waiting for response timed out after 90 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout.

at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TestRequestSender.CheckVersionWithTestHost()

at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings)

at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)

========== Test discovery aborted: 0 Tests found in 1.5 min ==========

========== Starting test run ==========

Failed to negotiate protocol, waiting for response timed out after 90 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout.

Failed to negotiate protocol, waiting for response timed out after 90 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout.

========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========”

Solution

Then I researched, spent several hours surfing various sites, forums and couldn’t get the solution for this. Later on, I checked and compared the project file of working test project after upgrade to .NET 6 and the project having issue after upgrade where I found some differences in the .csproj file.

Differences are listed below:

  1. Additionally, there was no Microsoft.NET.Test.Sdk in the .csproj file.
  2. Project which is having issue has different version of xunit and xunit.runner.visualstudio than the working project.

I updated the project file (csproj) manually and made it same as a working project, then cleaned and built the project. After that the test project worked perfectly.

Updated part of the project.csproj file is highlighted below.

Alternatively, you can add “Microsoft.NET.Test.Sdk” from the NuGet package manager or Package Manager console as well. I made changes to xunit and xunit.runner.visualstudio to make uniformity of version with all test projects.

Note that: some cases only adding the below line in ItemGroup may resolve the issue. In my case only adding the below package reference resolved the problem. However, to maintain the same version of xunit and xunit.runner.visualstudio, I kept the same version of those across all test projects.

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />

Test Project’s project file(.csproj file) in details

  • TargetFramework specifies the target framework of your project.
  • IsPackable has nothing to do. Unit test projects cannot be packed so it is redundant. If you wish you can remove it. However, when you create the test projects in .Net 6, it comes by default.
  • Packages xunit.runner.visualstudio and Microsoft.NET.Test.Sdk are mandatory to run the test project using visual studio as well as with command dotnet test.

In my case, Microsoft.NET.Test.Sdk was missing which plays a vital role to run the unit test that’s why I was not able to run tests.

I hope, this helps you to upgrade your test project to .NET 6 and resolve the issue if you encounter it after the upgrade.


Recommended Free Ebook
Similar Articles