How to do .Net core setup for tests

This is a short recipe for how to set up your project for testing .net core.

Test projects needs to target a platform, see the different possible platforms, and possible settings for frameworks and platforms.  The latter link show settings that can be applied to both the csproj file and the c# files.

A possible platform is .Net Core 2.2

Download .Net core 2.2.5 from here

Add nuget package Microsoft.Net.Test.Sdk to your project

A possible csproj file can look like:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework> 
  <LangVersion>latest</LangVersion>
</PropertyGroup> 

<ItemGroup> 
   <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" /> 
   <PackageReference Include="NUnit" Version="3.12.0" /> 
   <PackageReference Include="NUnit3TestAdapter" Version="3.15.1" /> </ItemGroup>

</Project>

Also see Rob Prousse blogpost on testing .net core, and  NUnit docs  for more details.

Traps:

  • Trying to use .Net Standard as target for your tests.  This will not work.  You need a platform target for your test projects.
    • Even if your test projects targets .net core, your production code, the code under test, can still be .net standard
  • Trying to use a Vsix test adapter to test .net core.  This will not work.  You need to use the nuget test adapter., and the version must be 3.8.0 or higher.
  • Forgetting to add the Microsoft.Net.Test.Sdk package. This does not come with the adapter, even if that might be a good idea, and might happen later on, but right now it needs to be added manually.
  • Forgetting to restart Visual Studio after adding an adapter.
  • Updating older projects and forgetting to update Microsoft.Net.Test.Sdk to the latest version,  see details here.