Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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=14.0.0.0, 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 xUnit.net), 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.

like image 470
Fred Kleuver Avatar asked Apr 10 '15 11:04

Fred Kleuver


People also ask

How do I run unit test cases in Visual Studio 2017?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

How do I get unit test coverage in Visual Studio?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window.

How do I run NUnit tests in Visual Studio 2015?

From within Visual Studio 2012, 2013 or 2015, select Tools | Extension Manager. In the left panel of the Extension Manager, select Online Extensions. Locate (search for) the NUnit Test Adapter in the center panel and highlight it. Click 'Download' and follow the instructions.


5 Answers

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.

like image 125
RobM Avatar answered Oct 19 '22 20:10

RobM


It could be that your code is compiled with x64 therefore have to enable the Default Processor Architecture as X64.

Test > Test Settings > Default Processor Architecture > X64
like image 21
Dac Toan Ho Avatar answered Oct 19 '22 19:10

Dac Toan Ho


EDIT 2016-10-19 (PowerShell script)

This issue still returns every now and then. I wrote a small PowerShell snippet to automate clearing the relevant cache/temp folder/files for me. I'm sharing it here for future readers:

@(
"$env:TEMP"
"$env:LOCALAPPDATA\Microsoft\UnitTest"
"$env:LOCALAPPDATA\Microsoft\VisualStudio\14.0\1033\SpecificFolderCache.xml"
"$env:LOCALAPPDATA\Microsoft\VisualStudio\14.0\1033\ProjectTemplateMRU.xml"
"$env:LOCALAPPDATA\Microsoft\VisualStudio\14.0\ComponentModelCache"
"$env:LOCALAPPDATA\Microsoft\VisualStudio\14.0\Designer\ShadowCache"
"$env:LOCALAPPDATA\Microsoft\VisualStudio\14.0\ImageLibrary\cache"
"$env:LOCALAPPDATA\Microsoft\VisualStudio Services\6.0\Cache"
"$env:LOCALAPPDATA\Microsoft\WebsiteCache"
"$env:LOCALAPPDATA\NuGet\Cache"
) |% { Remove-Item -Path $_ -Recurse -Force }

Make sure to close Visual Studio beforehand and it's probably a good idea to reboot afterwards.

Deleting the TEMP folder may not be necessary and may in some cases even be undesirable, so I would recommend trying without clearing the TEMP folder first. Just omit the "$env:TEMP".

Original answer 2015-04-12

The problem was "solved" after a thorough cleaning of Visual Studio-related temp/cache folders.

Since I did not have the time to go through everything one-by-one and then test in-between, I unfortunately don't know which one actually caused the problem.

These are the exact steps I've taken:

  1. Closed Visual Studio
  2. Used CCleaner to clear system and browser temp files/folders
  3. Manually cleared/deleted the following files/folders:

    • %USERPROFILE%\AppData\Local\assembly
    • %USERPROFILE%\AppData\Local\Microsoft\UnitTest
    • %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\14.0\1033\SpecificFolderCache.xml
    • %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\14.0\1033\ProjectTemplateMRU.xml
    • %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
    • %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\14.0\Designer\ShadowCache
    • %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\14.0\ImageLibrary\cache
    • %USERPROFILE%\AppData\Local\Microsoft\VisualStudio Services\6.0\Cache
    • %USERPROFILE%\AppData\Local\Microsoft\WebsiteCache
    • %USERPROFILE%\AppData\Local\NuGet\Cache
    • %USERPROFILE%\AppData\Local\Temp
like image 36
Fred Kleuver Avatar answered Oct 19 '22 18:10

Fred Kleuver


One reason for this problem is that your test class is not public. MSTest only discovers tests from public classes.

like image 22
AfshinS Avatar answered Oct 19 '22 19:10

AfshinS


  • Check out, if NUnit Test Adapter 2/3 is installed in VisualStudio.
    (Tools>Extensions and Updates )

  • Make sure that correct processor architecture is chosen:
    (Test>Test Settings>Default Processor Architecture)

like image 68
MichiBack Avatar answered Oct 19 '22 20:10

MichiBack