20

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?

valorl
  • 1,499
  • 2
  • 14
  • 30

8 Answers8

15

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 :)

Community
  • 1
  • 1
valorl
  • 1,499
  • 2
  • 14
  • 30
7

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>
Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
  • I didn't mention it but I did build, building does not help unfortunately. I edited my post with my `.csproj` file. The only difference from yours is the `OutputType` tag there. I tried adding it but no difference at all. – valorl Mar 12 '17 at 13:31
  • @valorl try add `OutputType` and rebuild. – Andrii Litvinov Mar 12 '17 at 14:03
  • So I restarted my PC, made a clean solution with a clean xunit test project, added the `Exe`, rebuilt and tried to `Run All`. No success... even had VS running as admin. I'll try to reinstall maybe it'll help – valorl Mar 12 '17 at 14:14
  • No success even after I have reinstalled... My VS version is `Community 2017, VisualStudio/15.0.0-RTW+26228.4` – valorl Mar 12 '17 at 14:41
  • If it helps, event when adding the `OutputType` line, I don't see any `.exe` file in `bin\debug\netcoreapp1.1` – valorl Mar 12 '17 at 15:00
  • @valorl, I see glitches in VS, but if I build/clean/rebuild project several time VS can eventually discover it. Try also add `ItemGroup` that I posted in second update to see if it hepls. – Andrii Litvinov Mar 12 '17 at 15:36
  • Sorry, I don't think I can help more. Please post a solution when you have it. Actually, try remove old .NET Core SDKs if you have those installed. Just a guess, because I removed them before installing VS 2017. – Andrii Litvinov Mar 12 '17 at 15:52
  • If you go to the command line and type "dotnet --version" what does it say? – Taylor H. Mar 12 '17 at 17:22
  • @Taylor It says `1.0.0` – valorl Mar 15 '17 at 13:35
5

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.

DGreen
  • 1,027
  • 1
  • 8
  • 17
  • 1
    Thanks DGreen. I also had an issue with VS 2017 discovering unit tests. While I had the correct framework referenced, your mention of the above command helped me discover that I did not have the unit test class access modifier set to public! – m2web Jun 11 '17 at 23:47
  • Thanks, I ran the command and got a warning that test class is nor marked as public, I added public keyword and now I can see my test. – Ali Apr 18 '20 at 10:55
2

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

pgoostree
  • 63
  • 6
  • I added the Microsoft.NET.Test.Sdk and it did not resolve my issue. – Francisco d'Anconia Jun 23 '17 at 18:48
  • I came here to share the exact same thing. I had one project among several in a solution that mysteriously just wouldn't show the tests. In the end, this is what solved it. So +1 from me. – Arwin Jan 27 '18 at 11:49
  • This worked for me when my tests stopped showing up after switching my project from .NET Framework v4.6.1 to .NET Core 3.1 – David Feb 03 '21 at 12:32
0

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...

Flaksirus
  • 31
  • 4
0

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

MiguelSlv
  • 14,067
  • 15
  • 102
  • 169
0

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>
coolhand
  • 1,876
  • 5
  • 25
  • 46
0

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

CodingYourLife
  • 7,172
  • 5
  • 55
  • 69