Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the xUnit Runner not finding my tests

I have a xUnit.net Test as follows:

static class MyTestClass {     [Fact]     static void MyTestMethod()      {     } } 

The xUnit plugin for VS 2012 says:

No tests found to run.

TestDriven.net runs it fine but mentions something about Ad hoc:

1 passed, 0 failed, 0 skipped (see 'Task List'), took 0.47 seconds (Ad hoc)

TeamCity, xunit.gui.exe and xunit.console.exe and Visual Studio also can't find TestMethod

(I've got xunit.runner.visualstudio installed and VS is seeing some tests.)

What gives?

like image 542
Ruben Bartelink Avatar asked Apr 25 '13 12:04

Ruben Bartelink


People also ask

Is xUnit a test runner?

The MSBuild runner in xUnit.net v2 is capable of running unit tests from both xUnit.net v1 and v2. It can run multiple assemblies at the same time, and build file options can be used to configuration the parallelism options used when running the tests.

Why is xUnit skipping tests?

When you have the ignore attribute above a testmethod the test will be skipped. Now when you run your tests you will see that this test has been skipped. Indicated by the yellow exclamation mark.

What test runners can be used to test xUnit.net tests?

If you have Visual Studio Community (or a paid-for version of Visual Studio), you can run your xUnit.net tests within Visual Studio's built-in test runner (named Test Explorer).

What is xUnit runner?

xunit.runner.devices. This package contains the devices runner. This runner can execute tests on physical and virtualized devices (iOS and Android via Xamarin, and UWP) from xUnit.net v2. To run UWP projects from within Visual Studio, please reference xunit.


2 Answers

TL;DR your Test Classes must be public (but your Test Methods can be private and/or static)


For reasons of efficiency, the xUnit authors have opted to not use BindingFlags.NonPublic when searching for Test Classes in the runner (the MSIL metadata tables don't index private(/internal) classes to the same degree hence there is a significant performance difference in the relative efficiency that Reflection can thus achieve).

As a result of the above, the fact that your class is private means it doesn't get picked up.

The fact that the Test Method is private and static is fine - xUnit by design since 1.0 has supported both those aspects.

Note that the Visual Studio xUnit Runner extension, xunit.console.exe (and the GUI), the xunit MSBuild task, Resharper and CodeRush are all consistent in honouring this (although arguably they [especially the latter two] could do more to flag when a Test Class (i.e. class [potentially indirectly] containing Fact-derived annoations) is private).

The reason TestDriven.net runs your test is that the Author of TestDriven.net has put great effort into making it Just Work. It internally uses a special Test Runner wrapper/shim (termed the Adhoc Runner) to run your test. Be aware that the method is actually not being run via the xUnit.net runner and hence any attributes you put on your test that have side effects will not be triggered.

Notably NUnit (and I'm pretty sure MSTest) do use private reflection [and hence pick up tests in private classes] which is probably why it never seemed an important thing for you to worry about before.

Note: A side effect / trick enabled by this is that you can make a Test Class private as a quick way of Skipping all tests in a Test Class [and any nested classes]. (Sadly the cases on this planet of this being used unintentionally vastly outnumber the intentional cases of this though!)

like image 182
Ruben Bartelink Avatar answered Oct 12 '22 12:10

Ruben Bartelink


This answer is for VS 2013, but the steps are essentially the same for VS 2012. This applies for running via ReSharper's unit test functionality.

  1. Install the xUnit.net runner for Visual Studio 2013 (be careful running Visual Studio as an Administrator, or the tests may not run when running the IDE as a non-Admin):

    a. From within Visual Studio 2013, go to Tools -> Extensions and Updates -> Online

    b. Search for xUnit.net runner for Visual Studio 2012 and 2013

    c. Then download (install) it. If upgrading to VS 2013 from VS 2012, it is suggested that this be uninstalled, and then re-installed.

    d. Restart Visual Studio.

  2. If ReSharper is installed, install the xUnit.net test runner plugin :

    (NOTE: Since ReSharper 2016.1, xunit support is built in to ReSharper, meaning the xunit plugin is no longer required.)

    a. In Visual Studio 2013, Navigate: Resharper -> Extension Manager.

    b. On the left, select Online.

    c. Search for “xunit.net”. Select the “xUnit.net Test Support”. Click Install.

    d. Restart Visual Studio 2013.

  3. “Clean” the solution

    a. In the IDE, in Solution Explorer, right-click the solution, and choose “Clean”.

    b. Re-compile.

    c. Now, when right-clicking a [Fact] attribute, select Resharper’s “Run Unit Tests” (as opposed to the default “Run Tests”)

Troubleshooting running with XUnit:

  • If problems running the [Fact] tests with XUnit persist, it might be necessary to manually remove the xUnit package from any/all of the following folders (review content for the xunit DLLs, then delete xUnit folder if found):

    • C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\

    • C:\Users\<username>\AppData\Local\Microsoft\VisualStudio\12.0\Extensions\

  • As for ReSharper, try un-installing and re-installing the xunitcontrib library (xUnit.net Test Support). I have noticed once when un-installing, some error messages flashing past. I grabbed a screen-shot at one point, and it listed:

    • Access to the path C:\Users\<username>\AppData\Local\JetBrains\ReSharper\vAny\packages\xunitcontrib.1.3.0\ReSharper\v8.1\plugins\xunit.dll is denied.
    • ... and the same for the other DLLs in that directory

To resolve this, delete the C:\Users\<username>\AppData\Local\JetBrains\ReSharper\vAny\packages\xunitcontrib.1.3.0\ directory after uninstalling from Visual Studio, then run Visual Studio as a non-administrator, and re-install via ReSharper (Resharper -> Extension Manager)

like image 30
CJBS Avatar answered Oct 12 '22 14:10

CJBS