20
votes

can anyone help me get VS2017 to work with .NET Core test projects?

I tried creating both MSTest and xUnit unit test projects for .NET Core from the VS 2017 templates. Neither of them works in the test explorer (not discovered), however running dotnet test from the project folder works fine.

Steps to reproduce:

  1. Create new project in VS 2017
  2. Choose either the Unit Test Project (.NET Core) or xUnit Test Project (.NET Core) template
  3. Implement some arbitrary unit test
  4. Build the solution
  5. Go to the text explorer and try to Run All

At this point the Output window should tell you that 0 test were discovered

.csproj file:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</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>

If I tried to create a MSTest unit test project from the template that targets .NET Framework (full, not .NET Core), it worked.

Any ideas?

8
The answer to stackoverflow.com/questions/40834871/… has helped me to resolve the issue. - Alexey Andrushkevich
That actually worked. Thanks a lot! - valorl

8 Answers

15
votes

In the end, the problem was solved by changing my system PATH env. variable from C:\Program Files\dotnet\ to C:\Progra~1\dotnet\, as answered in Can't get XUnit tests working with Visual Studio 2017 RC

Thanks to Alexey for suggesting it in the comments :)

7
votes

I think I experienced same behavior. Try to build your solution so that VS can discover your tests. Otherwise please share you tests csproj file to ensure you reference correct packages. Mine is:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</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>

UPDATE: I have played around a bit and it looks like VS cannot find the tests without <OutputType>Exe</OutputType>

UPDATE 2: Try also add following to csproj as I see VS adds in in some cases.

<ItemGroup>
  <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
5
votes

I hit this issue with VS 2017 not discovering MSTest unit tests on .NET Core.

The Tests output only reported that it had discovered zero tests.

On running dotnet test MyProject.csproj -t in a command prompt, it advised that I did not have the correct .NET Core runtime installed to match the project's RuntimeFrameworkVersion.

Installing the latest VS 2017 update and .NET Core runtime resolved the issue.

2
votes

I had the same issue. I resolved it by installing the "Microsoft.NET.Test.Sdk" package from nuget.

0
votes

I had the same issue after migration from project.json to csproj. And resolved it by removing net451 target framework and leave only netcoreapp1.1. Now it's works perfect and discover every time.

Of course if you need many framework targets you shouldn't do it, just use CLI to test them...

0
votes

It may due to inconsistent project settings for Processor Architecture. The Test project target must match the test default Processor Architecture.

Check https://stackoverflow.com/a/47454276/2700303

0
votes

I also had this issue and it was remedied by ensuring my project and test project had the same target framework version in the .csproj file

<TargetFramework>netcoreapp1.0</TargetFramework>
0
votes

Also make sure you don't have mixed PackageReference versions in the *.csproj files when having multiple test projects and the same output directory and also update the packages of test project especially MSTest.TestAdapter as well as MSTest.TestFramework