static void

VS2017 Unit Tests

Published Wednesday 22 February 2017

Test projects have changed a little in Visual Studio 2017. There are several new parts, including a new MsTest, in-the-box xunit (just for Core), and a new underlying test platform for mstest, xunit etc.

This post is written before the Visual Studio 2017 RTM (7 March 2017), and the core parts aren't working in Visual Studio. I guess they'll work at RTM, but in the meantime it's worthwhile pointing out the packages that make a unit test project. Update March 2017- it works fine in RTM

.net Framework

Under Add new project / Visual C# / test we have "Unit Test Project (.NET Framework)". This is the "classic" csproj with a packages.config pointing to the adapter and framework.

<packages>
  <package id="MSTest.TestAdapter" version="1.1.11" targetFramework="net461" />
  <package id="MSTest.TestFramework" version="1.1.11" targetFramework="net461" />
</packages>

The SDK is part of Visual Studio, so it's not referenced here.

This works pretty much exactly as it did before.

Remember with VS2015, we had a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll and no packages.config. Incidentally, if you open a VS2015 test project in VS2017, it doesn't do a conversion. It still references and runs against QualityTools dll (v10 for both VS2015 and VS2017, living in ...\Common7\IDE\PublicAssemblies\ for each visual studio).

Update March 2017: A "traditional" unit test project referencing QualityTools can test a NetStandard class library (because it implicitly supports .net 4.6), as well as a .net Core project that targets net46.

Core

In Visual Studio, we now have "Unit test project (.NET Core)" and the slightly differently named "xUnit test project (.NET Core)". The first is MsTest, the second is Xunit. These are the new csproj formats. First thing you can do is update the nuget packages to the latest versions.

The MsUnit csproj looks like this:

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

  <PropertyGroup>    
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.1.11" />
    <PackageReference Include="MSTest.TestFramework" Version="1.1.11" />
  </ItemGroup>

</Project>

The Xunit csproj is like this:

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

  <PropertyGroup>    
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>

</Project>

Unfortunately... it doesn't work in Visual Studio 2017 RC4. The xunit and mstest tests don't appear in the test explorer window, they don't get run from the VS menu commands, and there's an obscure "The active Tests Discovery was aborted" message in the output window. The sdk is still in -pre mode, so I guess we'll get a working version eventually... before VS2017 RTM in March I hope! Update March 2017: it works in VS2017 RTM.

The problem is definitely within Visual Studio, because the command line works fine. The new command is dotnet test. Tests are found and run. This is actually very nice.

Wrap up

Apart from .net Core tests not working in VS2017, this is a good direction for tests. Using nuget means the framework can evolve a little (mstest hasn't really moved since VS2010). The new csproj xml is very nice, even more concise than project.json/xproj. "dotnet test" is great.

Previously: C#7 More features (21 February 2017)