10

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.

Spikee
  • 3,967
  • 7
  • 35
  • 68
  • 1
    Do you have the "testRunner", and in dependencies, "dotnet-test-xunit" set in your project.json file ? – Kasun Koswattha Jun 15 '17 at 17:02
  • What is `testrunner` and `dotnet-test-xunit` cannot be found anywhere in my solution folder structure. – Spikee Jun 15 '17 at 17:07
  • What is the exact project type you use? .NET Core, or not? It is not an easy setup at this moment, due to the complexity of project types, so you must make it very clear what you are doing. – Lex Li Jun 15 '17 at 18:45
  • 4.6.1, updated. – Spikee Jun 15 '17 at 18:50
  • Have you tried [this answer](https://stackoverflow.com/a/43158783/5112433)? Does Resharper discover those tests? – Ilya Chumakov Jun 18 '17 at 18:21
  • if you're using Nunit you have to add the attribute [TestFixture] to your test class and [TestCase] to your test methods - i don't know the equivalent in xUnit but i'll bet you'll have to do something similar otherwise it won't pick up your tests – BenKoshy Jun 20 '17 at 08:39
  • 1
    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 Jun 21 '17 at 04:34
  • @Ayb4btu I'll check asap. Might be it. – Spikee Jun 21 '17 at 13:15
  • @Ayb4btu See update 2, everything seems to have been created as it should afaik. – Spikee Jun 21 '17 at 19:20
  • 1
    @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 Jun 22 '17 at 06:55
  • @Ayb4btu https://github.com/KristofBD/Aetheria.git - switch to the `dev/game-loop` branch. – Spikee Jun 22 '17 at 08:46
  • 1
    Did you try clearing cache as posted here? https://xunit.github.io/docs/getting-started-desktop.html#run-tests-visualstudio – brijber Jun 23 '17 at 14:06
  • @brijber I'll check when able :P. – Spikee Jun 23 '17 at 14:09

2 Answers2

13

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

Ayb4btu
  • 3,208
  • 5
  • 30
  • 42
  • That helped getting it working, thanks for the effort! – Spikee Jun 24 '17 at 09:26
  • so we were doing this in prod - but then we started testing a file that uses "System.Windows.Forms" and our .netCore2.1 TESTAPP blew up (can't load "System.Windows.Forms" from another assembly) – Poat May 13 '19 at 21:24
  • 1
    @Poat, I suspect that it is because winforms is not supported by .NET Core 2. Try using [.NET Core 3](https://devblogs.microsoft.com/dotnet/net-core-3-and-support-for-windows-desktop-applications/) (it is only in preview at the moment though). If you still have issues, I would recommend posting a separate question. – Ayb4btu May 13 '19 at 21:44
0

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

GangNanTed
  • 43
  • 1
  • 8