Visual Studio 2015 or 2017 does not discover unit tests


EDIT 2016-10-19:

The original question was about an issue specific to VS2015 CTP6 with the XUnit test runner. It's clear from the answers that there is a much broader issue with unit test discovery in Visual Studio which may occur in many different situations. I have cleaned up my question to reflect that.

I have also included a script in my own answer that I still use to this day to solve similar problems when they appear.

Many other answers have also proven helpful in better understanding the intricacies of the VS test runner. I appreciate that people are still sharing their solutions!

Original question 2015-04-10:

Since yesterday, my Visual Studio Test Explorer won't discover tests for any of my projects. It does not show the green loading bar after building, either.

When I go to the Visual Studio Test Explorer and click "Run All", or when I right-click any test method and select "Run Tests", I get the following in my output window:

Could not load file or assembly 'Microsoft.VisualStudio.Web.ProjectSystem, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I am running Visual Studio 2015 CTP 6 on Windows 10 Pro Technical Preview, build 10041. The .NET Framework version does not seem to matter – it happens on 4.0, 4.5.2 and 4.6.

I tried with the following testing frameworks and all of them give the same behavior:

  • Microsoft.VisualStudio.QualityTools.UnitTestFramework v14.0.22609.0
  • xunit v2.1.0-beta1-build2945 with xunit.runner.visualstudio v2.1.0-beta1-build1051
  • NUnit v2.6.4 with NUnitTestAdapter v2.0.0

I found an issue on GitHub (xunit) that appeared to be similar: Cannot get tests discovered #295, with this comment from the xunit team:

Be aware that Visual Studio 2015 CTP 5 has been reported to be broken
by many people with unit testing in general (not just, so
don't expect that to work.

Also, please make sure you've cleaned out Visual Studio's runner
cache. If it gets corrupted, Visual Studio will permanently misbehave
until it's deleted. To clear the cache, shut down all instances of
Visual Studio, then delete the folder
%TEMP%\VisualStudioTestExplorerExtensions (honestly, it probably
wouldn't hurt to delete everything in %TEMP% that can be deleted).

I tried their suggestion to delete the folder %TEMP%\VisualStudioTestExplorerExtensions. Unfortunately that did not fix the problem.

I noticed that ReSharper actually is able to discover some tests. It only works for the VS and NUnit tests, not for xunit.

There has to be some sort of temp or cache folder I need to clear, but I know Visual Studio has many of them and not all of them can be deleted without unwanted side-effects.

Best Solution

To my surprise, clearing temp files located in the %TEMP% directory resolved the issue for me.

Note: This path is generally at C:\Users\(yourusername)\AppData\Local\Temp

As @Warren-P included, you can navigate to the temp folder by putting in %temp% in Start Menu, or launch "File Explorer" and enter %temp% in the address bar.