VS 2017 not discovering tests on .NET Core
Asked Answered
N

9

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?

Nanette answered 12/3, 2017 at 13:14 Comment(3)
The answer to #40835371 has helped me to resolve the issue.Corner
That actually worked. Thanks a lot!Nanette
Possible duplicate of Unit Tests not discovered in Visual Studio 2017Atwitter
N
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 :)

Nanette answered 16/3, 2017 at 18:50 Comment(1)
Thanks! I'm running VS2017 Pro 15.1 and this was the only thing that fixed me.Sartin
L
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>
Lavatory answered 12/3, 2017 at 13:22 Comment(9)
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.Nanette
@Nanette try add OutputType and rebuild.Lavatory
So I restarted my PC, made a clean solution with a clean xunit test project, added the <OutputType>Exe</OutputType>, rebuilt and tried to Run All. No success... even had VS running as admin. I'll try to reinstall maybe it'll helpNanette
No success even after I have reinstalled... My VS version is Community 2017, VisualStudio/15.0.0-RTW+26228.4Nanette
If it helps, event when adding the OutputType line, I don't see any .exe file in bin\debug\netcoreapp1.1Nanette
@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.Lavatory
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.Lavatory
If you go to the command line and type "dotnet --version" what does it say?Amos
@Taylor It says 1.0.0Nanette
H
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.

Horned answered 17/5, 2017 at 12:31 Comment(2)
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!Lariat
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.Myelencephalon
U
2

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

Unveil answered 15/3, 2017 at 21:8 Comment(3)
I added the Microsoft.NET.Test.Sdk and it did not resolve my issue.Joktan
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.Lauzon
This worked for me when my tests stopped showing up after switching my project from .NET Framework v4.6.1 to .NET Core 3.1Pungy
M
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...

Mincemeat answered 3/7, 2017 at 11:10 Comment(0)
S
0

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

Check https://mcmap.net/q/117494/-unit-tests-not-discovered-in-visual-studio-2017

Swim answered 23/11, 2017 at 11:15 Comment(0)
S
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>
Selfsustaining answered 2/1, 2018 at 17:31 Comment(0)
A
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

Annadiane answered 7/8, 2018 at 21:32 Comment(0)
O
0

Note for testing with SpecFlow. If your solution has more projects you must include the package "Microsoft.NET.Test.Sdk" reference in each *.csproj

Obedient answered 18/1 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.