Fred Kleuver
Fred Kleuver

Reputation: 8037

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:


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.

Upvotes: 171

Views: 125492

Answers (30)

Mohammed Hameed
Mohammed Hameed

Reputation: 83

Please note that I have walkthrough over the current answers and did not find related to loading/unloading the test projects (the trick which worked in my case and I have already answered it on another similar question on SO here https://stackoverflow.com/a/68101958/966557).

Here we go:

I was also facing the same issue, as a workaround I Unloaded my test project and then Reloaded back; then ran the test again. This time it refreshed the Test explorer :)

My VS version: VS Professional 2019 Version 16.9.6.

Note: I had to do this to all my Test projects.

Upvotes: 0

Marin
Marin

Reputation: 73

I believe you already found the issue, but in my case helped to simply install the Microsoft.NET.Test.Sdk. Make sure you add it to your test project. I've spent few days trying to solve the problem and it's as simple as that.

Microsoft.Net.Test.Sdk should be installed no matter what testing framework you are using.

https://i.sstatic.net/Roof5.png

Upvotes: 0

themenace
themenace

Reputation: 2800

For me the solution was cleaning and rebuilding the Test Project

Build > Clean

Build > Build

I haven't read that in the answers above, that's why I add it :)

Upvotes: 0

Abhas Bhoi
Abhas Bhoi

Reputation: 387

  1. Update nunit.framework to latest version.
  2. Download nunit test adapter from nuget
  3. Restart Visual studio

Upvotes: 1

Dac Toan Ho
Dac Toan Ho

Reputation: 1113

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

Upvotes: 92

JerryM
JerryM

Reputation: 885

I solved this problem by realizing that the Target Framework for my test project was different than the project under test. Yes, I caused this problem by changing the target framework from the default (Project>Properties>Application), but failed to this this for the test project, which was created several weeks later. The mismatch did not cause a compiler error, but it did result in a warning in the Error List window. Once I selected the option to display warnings, the solution was obvious.

Upvotes: 1

atconway
atconway

Reputation: 21304

I had an instance where some tests would not be picked up because I had made them async like the following:

public async void This_IsMy_UnitTest()

The problem was I forgot to make them return a Task and not void when I did the switch-over. One would think this would cause an error or failed test but nope. The unit tests in that class were fully ignored and acted like they didn't exist.

It wasn't after about 3 clean and builds + restarting VS.NET that I saw the test run and fail indicating I forgot to add the Task return type:

public async Task This_IsMy_UnitTest()

After the update, the unit tests were found and worked correctly. This might be an edge case, but having async tests for using await within but not having the signature correct can cause this same issue and it's not the 1st time I've done this.

Upvotes: 9

TommyD
TommyD

Reputation: 781

We had the same problem. We have a big VS 2015 solution with multiple C# projects in it and even more test projects.

Resharper's test discovery worked just fine, but VS Test Explorer failed miserably.

Turns out that the projects didn't have the same version of MsTest TestFramework and TestAdapter, and that sometimes they used NuGets and other times good old references, and that is not supported apparently (so much for such an expensive IDE).

Removing all Microsoft.VisualStudio.Test* references and then adding / updating the two MSTest NuGets fixed the problem.

Upvotes: 1

Roland Roos
Roland Roos

Reputation: 1083

The only thing that worked for me was: Remove C:\Users(yourusername)\AppData\Local\Temp

Other suggestions typically are valid. But for some reason, if VS does not pick up your changes and keeps barking in your output it cant discover tests, cleaning this dir can do the trick. And, yes, it may be just "some day" you startup and none of your solutions will work anymore, wheras "yesterday" everything worked fine.

Upvotes: 0

mggSoft
mggSoft

Reputation: 1042

It was so easy for me to fix the issue as:

  • Select your Unit Test Project
  • Click on 'Show All Files' button in the Solution Explorer and new temporary files appeared in the file tree of the Solution Explorer within 'obj\x86\Debug'.
  • Delete these temporary files and rebuild project.
  • Retried to run tests and worked!.

Upvotes: 1

MichiBack
MichiBack

Reputation: 1430

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

Upvotes: 69

Ohad Schneider
Ohad Schneider

Reputation: 38106

This happened to me because my test project contained an app.config. It was automatically added by NuGet packages for assembly redirection, but my tests seemed to run fine without it.

See: https://developercommunity.visualstudio.com/comments/42858/view.html.

Upvotes: 2

csha
csha

Reputation: 9554

If you're targetting .NET Standard or .NET Core, you need to use the NuGet package for NUnit Test Adapter and not the extension.

It is recommended to install the adapter from NuGet if you are testing .NET Core or .NET Standard projects. The VSIX adapter does not, and will not, support .NET Core because VSIX packages cannot target multiple platforms.

Source: NUnit GitHub Wiki

.

Also check the FAQ there:

My tests aren't showing up in Visual Studio 2017?

  • Are you using the NuGet package?
  • Are you using version 3.8.0 or newer of the NuGet package?
  • Do your tests target .NET Core or the full .NET Framework? (see above)
  • Have you added a Package Reference to Microsoft.NET.Test.Sdk?
  • Have you restarted Visual Studio? It is still a bit tempermental.

Source: NUnit GitHub Wiki

Upvotes: 2

Maate
Maate

Reputation: 1000

Somehow my project was set to compile as a Static Library (.lib). After changing this to a Dynamic Library (.dll), tests where discovered correctly by Visual Studio 2012.

My Unit Test Project ->
Properties ->
Configuration Properties ->
General ->
Configuration Type

Upvotes: 2

Umar T.
Umar T.

Reputation: 421

The solution in my case was just to install the NUnit 3 Test Adapter extension to my Visual Studio 2015.

'Extensions and Updates' is present under 'Tools' meue

Upvotes: 5

Harsheet
Harsheet

Reputation: 628

For me upgrading to version 3.7 of NUnit worked.

Upvotes: -1

Scott Langham
Scott Langham

Reputation: 60341

In the VS Output pane (switched to the Test view), there was this error:

Could not load file or assembly 'XXX.UnitTest, Version=9.4.0.0, Culture=neutral, PublicKeyToken=14345dd3754e3918' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)

In the project settings for the test project, under the Signing tab, someone had checked 'Sign the assembly'. Unchecking that and building caused the tests to show up.

A colleague also solved the same issue by adding keys from this post to the registry:

https://blogs.msdn.microsoft.com/keithmg/2012/03/20/strong-name-validation-failed-exception-from-hresult-0x8013141a/

Upvotes: 0

Brad Johnson
Brad Johnson

Reputation: 91

Disable Windows Defender Service. Turning this off immediately caused all of my unit tests to show up in Test Explorer.

Upvotes: 0

curiousBoy
curiousBoy

Reputation: 6834

After spending 2 days... none of the above worked for me. The only "solution" was: Go to project properties -> Build Tab. Then click Advanced button on the right bottom corner of the pane. Change "Debug Info:" to "full" and click OK.

Here are the screen shots: enter image description here

enter image description hereenter image description here

Upvotes: 0

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 115

I had the same issue. The unit test template of Visual Studio 2015 (Update 3) generates a class with TestContext property defined as follow:

    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }

After changing it to a public field (ungly) the test runner could discover the test.

public TestContext TestContext;

Very strange behaviour, but it was the cause of the issue in my case.

Upvotes: 0

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

If you are working with multiple App or Web.Config files. eg:

enter image description here

Its likely you are using a Config that is RELEASE MODE and that will strip the Debug mode setting from the config:

  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />

Change the Config to one that doesn't remove DEBUG MODE.

Upvotes: 0

ScottishTapWater
ScottishTapWater

Reputation: 4776

I would just like to add that I found an entirely different solution to the ones above.

I had declared my test class as below:

[TestClass]
class ClassificationTests
{
   //unit tests
}

As soon as I added the public modifier to the class, it worked as expected!

Upvotes: 3

Pouyan Sepahvand
Pouyan Sepahvand

Reputation: 21

I resolved it by changing X64 to : Right click on project -> Properties -> Build -> Platform target -> Any CPU

Upvotes: 1

Marcus
Marcus

Reputation: 6107

I would like to add one further reason tests may not be found, in my case it pertained C++ unit tests that were not found.

In my case tests were not found for a particular project because its output directory was not contained within the project directory, changing this ensured the tests were found.

Upvotes: 0

RobM
RobM

Reputation: 3609

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.

Upvotes: 158

Khan
Khan

Reputation: 18142

Make sure your Test Methods do not have parameters. This is another way your test will not show up.

No Errors or Warnings.

Upvotes: 0

AfshinS
AfshinS

Reputation: 221

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

Upvotes: 22

Vishal
Vishal

Reputation: 615

I made the mistake of creating async methods but returning void.

Changed: public async void Test()

To: public async Task Test()

Upvotes: 0

pvasek
pvasek

Reputation: 1156

Ensure you have xunit.runner.visualstudio package in your test project packages.config and also that was correctly restored.

I know this was not the case of the original question however it could save time for someone like me.

Upvotes: 3

Robbie Dee
Robbie Dee

Reputation: 1977

I was also bitten by this wonderful little feature and nothing described here worked for me. It wasn't until I double-checked the build output and noticed that the pertinent projects weren't being built. A visit to configuration manager confirmed my suspicions.

Visual Studio 2015 had happily allowed me to add new projects but decided that it wasn't worth building them. Once I added the projects to the build it started playing nicely.

Upvotes: 1

Related Questions