65

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?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • related: http://stackoverflow.com/questions/16647789/unit-tests-appear-in-xunit-gui-xunit-gui-clr4-exe-but-not-vs-2012-test-explore/16657414#comment23976438_16657414 – Ruben Bartelink May 21 '13 at 20:30
  • related (VS2015): http://stackoverflow.com/questions/35103781/why-is-the-visual-studio-2015-test-runner-not-discovering-my-xunit-v2-tests/35103782#35103782 – Ruben Bartelink Sep 30 '16 at 10:58

9 Answers9

102

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

Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • 2
    I'm having same issue, my class is public, trying to use the MS Test runner with the Visual Studio Extension xUnit Test Runner with VS 2012 – Kyle Gobel Sep 01 '13 at 23:55
  • @Kyle It's not clear to me what you're doing and/or seeing as a result and/or how anyone can help you. I **had** an issue and you've commented on my *solution*. Firstly, note This question and answer are both about **xUnit.net**. You've mentioned MS Test, which relies on a built in runner (and I'm pretty sure MSTest does not allow Test Methods to be private). – Ruben Bartelink Sep 02 '13 at 09:31
  • 1
    sorry for random comment question, My problem was I had installed the pre-release nuget package, which doesn't include xunit.dll but xunit2.dll, so it wouldn't pick up any of the tests – Kyle Gobel Sep 02 '13 at 22:20
  • @Kyle ah, now that makes sense - at least enough sense to add as an answer so I can upvote it (I was personally was aware of that but I can guarantee someone will be bitten by it and will thank you for it) – Ruben Bartelink Sep 02 '13 at 23:38
  • My fact class is `public` as well, I tried both the beta and non-beta Nuget packages (for the Xunit.net and the runners), the Test Explorer does not find my `Fact` methods. – BrainSlugs83 Dec 14 '14 at 02:22
  • @BrainSlugs83 You sure you have the xUnit.net plugin for VS installed? What is the Tests area in the Output displaying regarding the discovery process ? – Ruben Bartelink Dec 15 '14 at 02:15
  • I was about to upvote this answer when I saw that I already had... probably a few months ago when I had done this exact same thing. Ugh. Thanks! – Todd Vance Dec 02 '16 at 19:40
  • After trying various other methods listed here and else where, this answer worked great for me. This answer should be in the top position. – dotcoder Jun 14 '17 at 07:39
  • @mohang thanks - I believe it _is_ in the top position (as I accepted it myself!). [I will delete this comment of mine when I see it next as it does not provide any value] – Ruben Bartelink Jun 14 '17 at 07:50
  • Not sure why this isn't bumped higher than the other answers. I had to do some scrolling... Stil trying to figure out why visual studio doesn't make public by default. – andrewCanProgram Aug 20 '17 at 16:07
  • @andrewCanProgram https://en.wikipedia.org/wiki/Principle_of_least_privilege - same reason `readonly` should have been the default for fields ;) I did accept the answer so it _should_ be top; maybe you've sorted by activity ? – Ruben Bartelink Aug 20 '17 at 21:04
42

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)

CJBS
  • 15,147
  • 6
  • 86
  • 135
  • 1
    The definite answer for VS 2013 + ReSharper 8.2 – Ognyan Dimitrov Apr 24 '14 at 15:00
  • 1
    For VS 2013 + R# 8.2 I found that restarting VS worked for me. This is after using R#'s extension manager to download and install the xUnit support – JasonCoder May 20 '14 at 17:49
  • Wish I could upvote more than once! I was missing the resharper extension :S – sǝɯɐſ Dec 05 '14 at 17:01
  • NB This does not actually directly address my question - the question is "why can no runner under any circumstances find the test (except TestDriven.net" as opposed to "what might be wrong on a machine that could make the runner not find tests which I *can* run in other places". I appreciate that this is a very complete answer which will help people that land here so am happy for it to remain regardless of that fact though – Ruben Bartelink Mar 10 '15 at 11:50
  • 1
    @RubenBartelink When I wrote this, I was having problems getting ReSharper to run my xUnit tests, and my output "No tests found to run." was the same as that described in your question. I didn't have TestDriven.net, so I couldn't validate either way. Many of my xUnit problems have been related to updates installed via NuGet or ReSharper, hence the instructions on how to deal with them. – CJBS Mar 10 '15 at 15:51
  • As I was saying, it's definitely useful for people, but *TeamCity, xunit.gui.exe and xunit.console.exe also can't find TestMethod* was a key aspect of my question. As I said tough, it's turned out well - people are obviously getting help from your answer – Ruben Bartelink Mar 10 '15 at 23:24
  • @RubenBartelink Point taken. – CJBS Mar 10 '15 at 23:25
  • 1
    @CJBS I guess the easiest way I could have expressed that is by changing the title to "Why is the xUnit runner not finding (some of) my test. On reflection though, a one stop shop question sometimes is exactly what people need even if it does go against some SO principles. – Ruben Bartelink Mar 11 '15 at 08:08
  • I had Resharper installed already and using XUnit..Followed point.2 mentioned by @CJBS, it worked like a charm. Thanks – WonderHeart Jul 30 '15 at 14:11
18

From http://xunit.github.io/docs/getting-started-desktop.html#run-tests-visualstudio:

If you're having problems discovering or running tests, you may be a victim of a corrupted runner cache inside Visual Studio. To clear this cache, shut down all instances of Visual Studio, then delete the folder %TEMP%\VisualStudioTestExplorerExtensions. Also make sure your project is only linked against a single version of the Visual Studio runner NuGet package (xunit.runner.visualstudio).

Dan Friedman
  • 4,941
  • 2
  • 41
  • 65
  • Hey Dan, I have a link in the related: on the question: http://stackoverflow.com/questions/35103781/why-is-the-visual-studio-2015-test-runner-not-discovering-my-xunit-v2-tests/35103782#35103782 which addresses this family of issues I'd really like to keep this question focused on reasons why xUnit can't see a test. I really hope you'll agree it that lnking to there and linking there is for the best. Is there a way I could have made this clearer in the question or my answer? (e.g. there are 3 other answers deleted here which were similar attempts to be helpful but would confuse matters) – Ruben Bartelink Oct 19 '16 at 08:42
  • I am getting the same error "No tests found to run." It may have been a different issue for you, but both situations end up in the same error. An alternate answer does not imply that there is a second question. If anything, this answer could be merged with the accepted answer to show all reasons why tests might not be showing up. – Dan Friedman Oct 19 '16 at 14:56
  • we'll have to agree to differ. The context is different - I v explicitly say VS2012 whereas you need to have a specific (rare) problem in VS2015 with xUnit v2 and not V1. There's 2 deleted answers that are more relevant that yours. Should I ask them to undelete (examples: resharper, .NET core beta, istalling xUnit.Runner.Console instead of desktop). Are you not happy there's a resharper version and a VS2015 version and let this be useful in 5 years time vs your answer competing with a VS2019 answer when yours will be completely irrelevant? – Ruben Bartelink Oct 19 '16 at 22:14
14

I had the same issue in VS2017 RC, .NET core 1.1 project. Updating xunit.runner worked for me,

Install-Package xunit.runner.visualstudio
Dhanuka777
  • 8,331
  • 7
  • 70
  • 126
  • Can you clarify whether you had the package installed in the first place? i.e., I believe the command above would need a `-Reinstall` flag for it to do anything you hadn't ? – Ruben Bartelink Mar 06 '17 at 09:40
  • Yes I had the package installed, this updated the xunit runner to 2.2.0 from 2.2.0-beta5-build1225 version. – Dhanuka777 Mar 06 '17 at 22:57
  • xunit.runner.console is not enough, but when i installed this, it worked – Alexander Jun 19 '18 at 10:13
6

(As referred to by @Kyle in the comments on the other answer) The same No tests found to run message can result from using NuGet to get xUnit.dll and ending up with version 2.0.0 (which is currently marked as prerelease as some core functionality like discovering of v1 tests etc. has yet to be implemented in that branch).

The resolution in this case is to select Stable Only versions (as opposed to Include Prerelease) in the NuGet package manager.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
RichardU
  • 91
  • 1
  • 4
  • This is a pretty silly choice of the xUnit devs, if they were going to make 2.0 so broken currently they should have renamed the entire project to xUnit2 not done this. – Chris Marisic Feb 12 '14 at 15:57
5

I've been having this issue with .NET Core for a while now where a test class or a test method is not being discovered. The following fix works for me:

  1. Open a command prompt window.
  2. Change to the project directory.
  3. Build the project running the following command:

    dotnet build
    

NOTE: Building from Visual Studio.NET will not work! <<<<<<<<<<< IMPORTANT!

  1. Run the tests: Test --> Run --> Test All - CTRL+R +A (this will discover the new test(s) - but not run the the new test(s).
  2. Run the tests again.
mkaj
  • 3,421
  • 1
  • 31
  • 23
  • Hi, Please note the question here is why is there a problem with just one TestClass. Please do this as a self - answer question and note that it is specifically a thing that can arise on .NET Core (Pre-release specific) - There's two upvotes in it for you, whereas I really don't think it's helpful on thsi specific question – Ruben Bartelink Dec 07 '16 at 09:53
  • I've updated my answer as it it for one class, or just one method it won't find. Interesting that you note it's just .NET Core pre-release specific. We haven't upgraded yet. Next on the cards. – mkaj Dec 07 '16 at 19:11
  • Only slightly swayed. I guess upvotes here will tell whether anyone else considers this the right place (remember, the title is why is _the xUnit runner_ not finding my tests - there are lots of VS2015-specific questions (or are you using VS2017 ?)) – Ruben Bartelink Dec 07 '16 at 21:45
  • I'm not bothered about up votes. I've posted here to provide additional options for other punters - should they have the same issue. This is how I discovered the fix. I'm using .NET Core pre-release with VS2015. – mkaj Dec 08 '16 at 03:37
  • My point regarding upvotes is that they measure relevance of an answer to a) the question b) the search criteria that can yield the question. I'm saying b) might be the case despite a) not being the the case. I confidently agree you have not given so detailed an answer based on a crazed desire for fake internet points and are acting in best faith; I am however respectfully continuing to argue the case that this answer is not in its most relevant possible location (NB there are 3 other answers that have deleted by authors here: 1xResharper, 1xDnx, 1x not installing `xunit.runner.visualstudio`) – Ruben Bartelink Dec 08 '16 at 06:02
  • Was having a similar issue with VS2019 displaying the test pass/fail icons over the method being tested. Selecting Test --> Run --> Test All - CTRL+R +A as indicated in this answer resolved issue for me. – nbstrat Jul 19 '21 at 16:06
4

In my case, in order to see any tests, I had to complete the following steps:

(All installed through NuGet Package Manager)

  1. Install xUnit v2.0.50727
  2. Install xUnit.extensions v2.0.50727
  3. Navigate to the following link and follow the steps outlined in the documentation: http://xunit.github.io/docs/running-tests-in-vs.html

I'm using Visual Studio 2013 Premium. (Resharper NOT installed)

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
4

For me, the combination of my test class and test method names were too long; xUnit appears to have some cap on this combination.

Shortening the name of just the test method allowed xUnit to discover that single test. Shortening the name of the entire class allowed xUnit to discover all tests in the class.

Threshold of class name + method name appears to be 172 characters.

StoriKnow
  • 5,738
  • 6
  • 37
  • 46
  • +1 Any messages in the the output window under test discovery when it breaks? if so would be worth adding to post if there are – Ruben Bartelink Aug 10 '16 at 00:13
  • 3
    Unfortunately not. It just said "No tests found to run". I wasn't having issues until I added a class with a questionably long name and test method, and put two and two together. – StoriKnow Aug 10 '16 at 00:16
0

My problem was that I updated xunit.runner.visualstudio to version 2.4.5. However, the project I am working for, is for .NET Standard 2.0. Therefore, I had to downgrade to version 2.4.3 of xunit.runner.visualstudio, since it supports ".NET 2.0 or later". But since version 2.4.4, ".NET Core 3.1 or later" is supported.

Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66