Why is the Visual Studio 2015/2017/2019 Test Runner not discovering my xUnit v2 tests

201,319

Solution 1

  1. Eliminate discovery exceptions from your inquiries; go to the output Window (Ctrl-Alt-O), then switch the show output from dropdown (Shift-Alt-S) to Tests and make sure there are no discovery exceptions

  2. Test|Test settings|Default processor architecture can help if your tests are x86/x64 specific and discovery is triggering bittedness-related exceptions, i.e. not AnyCpu

  3. As suggested in this answer(upvote it if the technique helps) running the desktop console runner (instructions) can be a good cross check to eliminate other possibilities, e.g. mangled config files:-

> packages\xunit.runner.console.2.2.0\tools\xunit.console <tests.dll>

NOTE The xunit.runner.console package is deprecated - when you get stuff working in VS, you'll be able to have dotnet test run them in CI contexts too


Go read the documentation - it's comprehensive, up to date, includes troubleshooting info and takes PRs:-

Important note: If you've previously installed the xUnit.net Visual Studio Runner VSIX (Extension), you must uninstall it first. The Visual Studio runner is only distributed via NuGet now. To remove it, to go Tools > Extensions and Updates. Scroll to the bottom of the list, and if xUnit.net is installed, uninstall it. This will force you to restart Visual Studio.

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

The following steps worked for me:

  1. (Only if you suspect there is a serious mess on your machine - in general the more common case is that the visual studio integration is simply not installed yet)

Do the DEL %TEMP%\VisualStudioTestExplorerExtensions as advised :-

PS> del $env:TEMP\VisualStudioTestExplorerExtensions
  1. Install the NuGet Package xunit.runner.visualstudio in all test projects

    • Paket:

        .paket\paket add nuget xunit.runner.visualstudio -i
      

      You need to end up with the following in your paket.dependencies:

      nuget xunit.runner.visualstudio version_in_path: true
      

      Note the version_in_path: true bit is important

    • Nuget: Go to Package Manager Console (Alt-T,N,O) and

      Install-Package xunit.runner.visualstudio
      

    Rebuild to make sure xunit.runner ends up in the output dir

  2. Close Test Explorer <- this was the missing bit for me

  3. Re-open Test Explorer (Alt-S,W,T)

  4. Run All tests (Ctrl R, A)

Solution 2

I had to change the Test Settings after changing the test projects CPU to x64. Then the tests where detected again.

Architecture

Solution 3

None of the above solutions worked for me (dotnetcore 1.1, VS2017). Here's what fixed it:

  1. Add NuGet Package Microsoft.TestPlatform.TestHost
  2. Add NuGet Package Microsoft.NET.Test.Sdk

Those are in addition to these packages I installed prior:

  • xunit (2.3.0-beta1-build3642)
  • xunit.runner.visualstudio (2.3.0-beta1-build1309)

Solution 4

Install xunit.runner.visualstudio package for the test project

Solution 5

Make sure that your test class is public.

Share:
201,319

Related videos on Youtube

Raymond
Author by

Raymond

I minimise the biggest liability in any system: Code, mostly by talking to customers so we achieve what they mean. I've been working hard on only writing it when in the Red and keeping it composable since 2006, but I have a few decades left to perfect the art... Mail: my first name at my second name dot com Work: Jet, ProductFitter, InishTech linkedin / +rbartelink / facebook

Updated on April 28, 2022

Comments

  • Raymond
    Raymond about 2 years

    UPDATE: Adding a 2019; the discovery/runner integration mechanism is same as per 2017 & 2015, so the key things that can go wrong are the same.


    I've read Why is the xUnit runner not finding my tests, which covers reasons xUnit would never be able to find your tests but my problem is different - I'm confident there's nothing subtle going on with my tests; (they have worked in other environments, this seems to be just my machine) - the Visual Studio Test Runner in Visual Studio 2015 [Community Edition] is simply not showing any of my tests. I'm not doing anything remotely exciting; the tests target xUnit.net v2 on the Desktop.

    I've looked in the Output window and am not seeing anything in at all under Test in the Show output from tabs.

    • Raymond
      Raymond over 7 years
    • Niklas Peter
      Niklas Peter over 7 years
      It is just one possible problem, but obviously you have to compile the test project before it will be shown in the test explorer.
    • Raymond
      Raymond over 7 years
    • Prisoner ZERO
      Prisoner ZERO almost 6 years
    • Bendram
      Bendram almost 4 years
      Installing Xunit.Runner.VisualStudio has resolved my problem
    • Renju
      Renju about 3 years
      Installing Xunit.execution.desktop solved the issue for me
    • Divan
      Divan about 3 years
      I had this problem, by forgetting to make my new test class public.
  • Esen
    Esen almost 8 years
    "Close Test Explorer <- this was the missing bit for me" This is the most important step that I missed as well and spent 5 hours to figure out. Thank you, I should have paid attention to the steps :)
  • Raymond
    Raymond over 7 years
    "but reported a useful error" ... which was ? Also can you verify it was definitely not listed in the discovery errors window as stated in the OP - i.e. can you confidently state "I've looked in the Output window and am not seeing anything in at all under Test in the Show output from tabs." ?
  • Tom Makin
    Tom Makin over 7 years
    See updated answer, I will post the return code info later on in case it is relevant.
  • Raymond
    Raymond over 7 years
    any telltale messages when you look in the output window with Tests selected in the dropdown?
  • Liam
    Liam over 7 years
    None at all, it just says No tests found
  • ZZZ
    ZZZ about 7 years
    the xunit VS runner was working for my sln. However, it had stopped showing up one day. Found out that I did not installed the VS running, and it was working probably because of the cache, left by the other sln which has the runner installed. After installing the runner, things work fine again. So don't forget to install the runner for every sln.
  • Ognjen Babic
    Ognjen Babic about 7 years
    This helped me, i was missing xunit.runner.visualstudio package.
  • Raymond
    Raymond about 7 years
    Did you see a message before that in the test discovery output?
  • Max
    Max about 7 years
    No I didn't see any errors, it took a while to figure out.
  • Raymond
    Raymond about 7 years
    hmm; strange (see top of my answer - it explains where to look; normally this is flagged (though there are def cases where there simply are no messages anywhere))
  • Raymond
    Raymond about 7 years
    sounds painful. Thanks for sharing and hope it helps someone some day. However I have to say: I can't think of any reason for the debug info level to affect the discovery process so I can only say "I don't think that was what actually shot the bear" - let's hope I'm wrong though ;)
  • Raymond
    Raymond about 7 years
    I'd suggest asking a self answered question as I did is the best way to sequester such info - feel free to copy paste my entire question and s/xUnit/MSTest/ if you think it makes sense ;)
  • t3chb0t
    t3chb0t about 7 years
    This is unbelievable. Deleting the folder that had three other empty folders in it fixed the issue.
  • Raymond
    Raymond about 7 years
    @t3chb0t It probably had 50 hidden files and a wierd permission ;) (maybe it was created by a different user identity?)
  • Tony D
    Tony D almost 7 years
    Your first sentence was the key for me. The Test window showed I had the wrong version of NUnit.
  • Raymond
    Raymond almost 7 years
    I would upvote this if it was on stackoverflow.com/questions/16214684/… as this is not xunit2 specific and a good checklist element
  • Raymond
    Raymond almost 7 years
    Hmmm. If you turn it back on, does it immediately fail again?
  • MartinJH
    MartinJH almost 7 years
    I missed the public keyword on my test class -_-
  • Raymond
    Raymond almost 7 years
    @martinJH I have a self answered one for that (Linked in OP) :- stackoverflow.com/questions/16214684/… ;) Not revealing how I discovered that though
  • chrisdrobison
    chrisdrobison almost 7 years
    Yep. If I close the solution, turn on the profiler and get back in, test detection fails.
  • sebas2day
    sebas2day almost 7 years
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv /resetuserdata could also fix your problem in my experience
  • Raymond
    Raymond almost 7 years
    @seb2day not seeing any info re that suggesting a direct connection so will not edit in for now (it seems TFS Cache related blogs.msdn.microsoft.com/ejarvi/2005/07/14/… and is not documented in 2015 or 2017 anymore, although there is a resetsettings docs.microsoft.com/en-gb/visualstudio/ide/reference/… )
  • curiousBoy
    curiousBoy almost 7 years
    @RubenBartelink completely agree with you, that is why I mentioned "solution" in a quote :) but weird enough, it worked right away once I did this.
  • Rich
    Rich almost 7 years
    Closing and reopening Test Explorer helped me, but it didn't work the first time. I can usually get test explorer to discover tests but I haven't figured out what consistently works.
  • Raymond
    Raymond over 6 years
    I'd really prefer if this was not here as this is about otherwise correct tests that can't be found in a particular instance of VS. I have a self-answered one re why an xunit test can't be discovered: stackoverflow.com/questions/16214684/…. May I suggest you create a why cant my MSTest test be picked up (by VS if you want). (As you prob know, this particular concern does not even apply to xUnit which is another reason why, helpful as it is, your answer does not belong here)
  • Raymond
    Raymond over 6 years
    Can you open a clone of this question and self answer please as I have done? This one is about xUnit v2 and later. Even xUnit v1 answers have no place here. You can link to it with a see also at the top of the question, or I can link to it in the question
  • Venky
    Venky over 6 years
    This solution works.. Otherwise everytime i had to remove %TEMP%\VisualStudioTestExplorerExtensions and still sometimes i had to run the tests from console.
  • Youcef Kelanemer
    Youcef Kelanemer over 6 years
    Using Visual studio 2017 and after upgrading package to xunit.runner.visualstudio.2.3.1, Xunit tests appeared next to Unittests.
  • Vanice
    Vanice over 6 years
    Removing %TEMP%\VisualStudioTestExplorerExtensions did it for me
  • VSO
    VSO over 6 years
    This fixed it for me. I don't know why this helps, but it does.
  • Babulaas
    Babulaas over 6 years
    Thank you very much. This was also the fix for me :)
  • Tomas Hesse
    Tomas Hesse over 6 years
    Oh, and check the Test target Architecture. x86 or x64 and make sure project and references are inline with your choisce
  • Raymond
    Raymond over 6 years
    Hm, not sure I buy the uninstalling and reinstalling packages or the need for Microsoft.TestPlatform.TestHost or Microsoft.NET.Test.Sdk but I appreciate the frustration, the documenting of stuff you did in order to reach the endline in your context (will +1 should I manage to verify this as a specific step that makes a difference)
  • Raymond
    Raymond over 6 years
    Can you mention your VS version in this ? While I'm trying to keep the question and my self-answer version-independent, I suspect anyone reading this and guessing whether it's worth a shot would benefit from that context being edited into your answer.
  • Tomas Hesse
    Tomas Hesse over 6 years
    I can add that today it DID NOT work at all!! It is VS 2017 Enterprise. I gave it up today. I unloaded the project and the test will not be run. There is probably something wrong with the project itself. Read somewhere it could be due to async tests in combination with exceptions (NotImplemented) maybe but I'm done with it. There's no valueable info.It just hangs on discovery and some times I have seen the test but not today.
  • Raymond
    Raymond over 6 years
    That sound like a proper pain. If discovery is actually hanging (ie. Test window of Output panel is showing no completion and no errors), it may be useful to attach another copy of VS (via attach to process, Ctrl-Alt-P) to the vstest.discovery.exe host exe within which that takes place. However I suspect that there should be some form of timeout and have not ever heard of anyone actually doing something like this. I hope your problems get simpler some time soon :(
  • dpberry178
    dpberry178 over 6 years
    I am using NUnit and I solved this issue by updating NUnit3TestAdapter to the latest version via NuGet.
  • Rob
    Rob over 6 years
    Thanks for this - in my instance just had to Update-Package -reinstall MSTest.TestAdapter and the tests got picked up.
  • RB Davidson
    RB Davidson over 6 years
    Best answer on the page. Fixed my problem.
  • user2023861
    user2023861 about 6 years
    I had to clean --> rebuild after changing.
  • geisterfurz007
    geisterfurz007 about 6 years
    Please extract your (at least seemingly) new question to its own post to make this a clear answer :)
  • Raymond
    Raymond about 6 years
    ... Entitled "why is the NUnit TestAdapter v3 not seeing my NUnit v2 tests? and a) ping here b) an put a "see also <link>" at top (even if it is slightly tenuous), but I'd like this answer removed as it does not fit well with the xUnit v2 tests in the title.
  • Hinrich
    Hinrich almost 6 years
    Really weird stuff, but deleting %TEMP%\VisualStudioTestExplorerExtensions and restarting VS finally worked!
  • Danny Su
    Danny Su over 5 years
    Thanks mate! My issues solved by Point 3. I can't believe that the testing framework won't change accordingly.
  • deadlydog
    deadlydog almost 5 years
    For some reason I had to use the keyboard shortcut to run all tests (Ctrl+R,A) before the Test Explorer would recognize my tests. For some reason clicking the Run All button with the mouse wouldn't find any tests.
  • ΩmegaMan
    ΩmegaMan almost 5 years
    My .NET 4.72 MS Test project needed TestPlatform.TestHost only when migrating from VS 2017 to VS 2019.
  • Raymond
    Raymond over 4 years
    thats explicitly addressed in the first proviso (I link t another question that covers that case); this Q+A is only about troubleshooting how how normally OK tests that work in other places dont work for someone righjt now in a given environment. For me this answer just confuses matters as it dilutes that distinctino.
  • Sebastian
    Sebastian over 4 years
    Note that on my machine with VS 2019 pro there was no %TEMP%\VisualStudioTestExplorerExtensions folder. Instead I had to delete `.vs` in the solution root
  • Piedone
    Piedone over 4 years
    In my case, Test Explorer was hanging on a previously failed test. If I browsed to it and tried to right click -> Run or anything then the whole VS would hang. Just doing a Clean and Rebuild cleared the test status and fixed the issue for me.
  • Piedone
    Piedone over 4 years
    May I add that simply building (i.e. F6) won't help, you need to right-click on the solution in VS Solution Explorer and click Rebuild Solution.
  • Abdul Saboor
    Abdul Saboor over 4 years
    MSTest did not find my tests because the class access modifier was internal.
  • datchung
    datchung about 4 years
    This solved it for me. In the Output window, select Tests from the drop down => see message "missing TestHost" message.
  • Raymond
    Raymond almost 4 years
    Please remove - this question is about xunit, and even if it mentioned the Xunit attributes, that's excluded in the question
  • Eric Aya
    Eric Aya over 3 years
    This solution has already been posted on this page. Please don't add duplicates.
  • Raymond
    Raymond over 3 years
    Hi, I excluded this reason in at the start of the question - this one: stackoverflow.com/questions/16214684/… is a similar one about why it doesn't work for anyone. This question is about "why is in not working on my machine, it normally works" ?!
  • Alex Ayscough
    Alex Ayscough over 3 years
    Already installed, but upgrading the version through nuget got it working again.
  • Relevant
    Relevant over 3 years
    This is what my issue was. Thanks!
  • joelmdev
    joelmdev about 3 years
    Both links are dead.
  • vullnetyy
    vullnetyy about 3 years
    @joelmdev thanks for the comment. I updated them
  • Raymond
    Raymond about 3 years
    This would prevent it working on any machine (the OP excludes such reasons); such reasons are catalogued in stackoverflow.com/questions/16214684/… - can you place the answer there instead please?
  • Ajay Kopperla
    Ajay Kopperla about 3 years
    I agree, but this is what i faced in my scenario and none of them worked for me and finally realized that i had accidentally changed. i know this is very rare case but happened to me
  • Raymond
    Raymond about 3 years
    Yes, but please log that as an answer to the other question linked in this question! It answers that question, it does not answer this. This question says "it works for everyone but be, what could it be". The other one says "my test wont work no matter what I do"
  • yogibear
    yogibear over 2 years
    Thanks, I will check it out as soon as I get the opportunity. If it works will let you know.
  • Raymond
    Raymond over 2 years
    This set of reasons, which will make them not work on anyone's machine anywhere (except TestDriven.net will half-run them) is covered under the question linked to in the OP: stackoverflow.com/questions/16214684/…
  • AbuBakar Khan
    AbuBakar Khan over 2 years
    Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review
  • lyolikaa
    lyolikaa over 2 years
    Precise solution, works for VS 2019
  • John
    John over 2 years
    Unfortunately I don't have the rep to post an answer. However, I just want to say that I encounter this issue intermittently. I did a Close Solution, then I reopened the solution, and my tests were discovered. So that's one more thing for people to try if this is just happening intermittently.