Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does xunit.runner.visualstudio not work for Visual Studio

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:

  • why-is-the-xunit-runner-not-finding-my-tests
  • this response
  • and this similar one
  • xunit.runner.visualstudio not working on Visual Studio 2013 Update 4

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.

like image 978
Spikee Avatar asked Jun 15 '17 16:06

Spikee


People also ask

How do I run xUnit test cases in Visual Studio 2019?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

How do I debug xUnit tests in Visual Studio 2019?

To start debugging: In the Visual Studio editor, set a breakpoint in one or more test methods that you want to debug. Because test methods can run in any order, set breakpoints in all the test methods that you want to debug. In Test Explorer, select the test method(s) and then choose Debug on the right-click menu.

Does xUnit work with .net 5?

Sometimes, you want to write tests and ensure they run against several target application platforms. The xUnit.net test runner that we've been using supports . NET Core 1.0 or later, . NET 5.0 or later, and .

Is xUnit compatible with .NET framework?

It's an open source unit testing tool for . Net framework that's compatible with ReSharper, CodeRush, TestDriven.Net, and Xamarin. You can take advantage of xUnit.Net to assert an exception type easily.


1 Answers

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:

  • Unit Tests not discovered in Visual Studio 2017
  • Visual Studio 2017 RC does not detect nunit tests
  • running xunit tests included in a .NET Standard 1.6 library

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

like image 200
Ayb4btu Avatar answered Oct 01 '22 02:10

Ayb4btu