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.

A possible platform is .Net Core 2.0.0

Download .Net core 2.0.0 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.0</TargetFramework>
 </PropertyGroup>

<ItemGroup>
 <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
 <PackageReference Include="NUnit" Version="3.8.1" />
 <PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
 </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.