10
votes

I've done a nuget install like this:

Install-Package xunit.runner.visualstudio -Version 2.2.0

On my test project.

I have a test similar to this:

public class When_Doing_Stuff_I_Want_To_Test
{

    [Fact]
    public void Can_Do_Stuff()
    {
        var result = DoStuff();

        result.ShouldNotBeNull();
        result.Success.ShouldBeTrue();
    }
}

Although that I have done numerous VS restarts, laptop reboots, left a day in between, VS 2017 is still not able to discover my tests:enter image description here

What can I do to fix this and see my tests?

Addendum

I'm working under 4.6.1, so not yet Core.

Questions regarding the same topic that did not help:

So there's a lot going round, none of it helped ...

Update

I can't get NUnit to work either, won't show up in test explorer as well.

Update 2

I wiped my project and recreated the projects like so:

enter image description here

Then I copied my original code and added all necessary references, no difference.

2
Do you have the "testRunner", and in dependencies, "dotnet-test-xunit" set in your project.json file ? - Kasun Koswattha
What is testrunner and dotnet-test-xunit cannot be found anywhere in my solution folder structure. - Spikee
You are definitely using 4.6.1? When creating a new project in VS2017 (New Project->Templates->Visual C#) you selected .NET Standard and not .NET Core (it will still say .NET Framework x.y.z up the top, even though a Core project is created)? I ask, as I have to run dotnet restore to get my xUnit tests to show at present. - Ayb4btu
@Spikee can you make your project available somewhere, and I'll attempt to get it running. This will help see if it is a problem with visual studio or with the solution. - Ayb4btu
Did you try clearing cache as posted here? xunit.github.io/docs/… - brijber

2 Answers

13
votes

After trawling the internet, it appears that targeting NETStandard.Library can't be used for test projects...

These were the links I got information from:

What can be done is have the project you want to test use NETStandard.Library and have your test project target Microsoft.NETCore.App. Your test project can then reference the project to test and the tests will run.

The .csproj for your test project will look something like this (versions might change):

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

The project to test would be this:

<PropertyGroup>
  <TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>

Lately I've had to run dotnet restore on the solution to get the tests to run, so you may need to do the same. Turns out .NET Core SDK 1.0.1 was being targeted instead of 1.0.4 (I didn't have the x86 version installed).

0
votes

Go to menu Tests -> Test Settings -> Default process architecture -> change it according to the platform target.