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

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

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

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

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

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