ChatGPT解决这个技术问题 Extra ChatGPT

Tests not running in Test Explorer

I am currently working on a solution that has currently 32 Unittests. I have been working with the resharper test runner - which works fine. All tests are running, all tests are showing the right test outcome.

However, the tests are not running when using the Visual Studio test explorer.

The Test Explorer is showing all unit tests, but once clicking on "Run All", all tests are getting greyed out and show no result of the test run:

https://i.stack.imgur.com/HfTGo.png

All test classes are public

All test classes are having the [TestClass] attribute declared

All test methods are using the [TestMethod] attribute

Both the productivity code and the test projects are targeting .NET 3.5.

I have already tried to clean build my solution, and / or delete all obj, bin, Debug and Release folders

I'd appreciate any hints which would explain this behaviour.

Update your ReSharper. Refer to this stackoverflow.com/questions/16011775/…
Wouldn't it be wonderful if VS said why a test was not run? Mismatched target platform, missing test adapter, ... :(
Where is it mentioned they use ReSharper ?
@BernhardHofmann I encountered an issue just like this right now and it turns out that VS was spitting out some error messages in Output > Tests. Important. Frustrating that VS doesn't make them more visible, but that's a place to look in a situation like this.
Year is 2020, 5 years since this questions, and still after trying 10 different answers, I couldn't get the nunit tests to run. Creating a console project to run the test code. sigh

J
Joseph Simpson

If your projects aren't all AnyCpu then you may also want to check that the following 2 settings match:

[Right click test project] -> properties -> Build -> Platform target - e.g. x64 [Main Menu] -> Test -> Test Settings -> Default Processor Architecture -> X64

I found that when these didn't match my test project would silently fail to run.


this one fixed my issues (switching from mixed platforms to anycpu)
make sure your testprojects are not netstandard2.0 but netcoreapp2.2
Worked. I really with VS would, you know, give us a clear warning or error message or something.
Temporarily changing the values and changing them back to Any CPU followed by hitting Save fixed it for me. Using VS 2019 Community Edition, in a project that was originally created in VS 2017.
VS2019 version: Main Menu -> Test -> Processor Architecture for AnyCPU Projects -> X64.
K
Kevin Mills

I had to change my async test methods to return Task instead of void.

The tests were then active and runnable in Test Explorer.


There is no way to wait for an async void method to finish. Async void is "fire and forget". That's why they cannot be tested.
The facepalming I did when I read this echoed over the land... Thanks for pointing out my stupidity. That worked for me. Upvoted
Although filed under the "no duh" category. I wish this could get up-voted more as its super obvious that your return type must be Task but easy to forget (because seemingly no other unit test in the world returns anything but void) and sends you down the rabbit hole of fixing your unit test configuration when there is no actual configuration problem. Really wish this just caused a compiler or the very least throws a runtime error.
@pat8719, sounds like a good idea for a code analyzer. They're not too hard to write. I just wrote out an xunit fact doing async void Foo() and their analyzers didn't complain, but they could certainly add one that would. cc: @bradwilson
This was my problem, too. In VS 2019, it doesn't seem to happen with Xunit, though. But it always occur when using MsTest + async void.
m
malat

If you are using NUnit rather than MSTest then you will need either the NUnit 2 Test Adapter or NUnit 3 Test Adapter for Visual Studio ≥ 2012/2013.


This helped me figure that that i needed to install the "NUnit 3 Test Adapter": marketplace.visualstudio.com/…
In VS 2019 worked for me to install the adapter from the package manager nuget.org/packages/NUnit3TestAdapter
According to this page you also need to install Microsoft.NET.Test.Sdk from Nuget
In VS 2019 I also installed the Extension Nunit 3 Test Adapter and it worked. One note: I also needed to update Visual Studio.
+1 Thank you! Even with the latest version of VS 2019 16.5.5 I got no message at all that indicated what was wrong. It silently failed.
P
Pablo Rausch

I had the same problem in VS 2017. In my case it solved by restarting VS.


my previous comment for why I gave this downvote was deleted, so I'll be a little clearer. This isn't a solution for the problem; it's a very temporary fix that doesn't solve the underlying issue. I don't think SO should be a place people should be coming to be told "turn it off and on again", that is knowledge to be learned way before you get here. Please see Joseph Simpson answer for a solution.
@lukkea I followd Joseph Simpson's answer and confirmed, that all my projects where AnyCpu. I still had the problem described here. For me simply restarting VS helped. That is not the first time VS2012 has these kind of hickups (and not only on my machine). I agree with you, that this isn't a good solution, but if there is nothing wrong with the project, that might be the only help. One should check if cleaning an rebuilding doesn't help either, though.
If only 'turning it on and off again' would fix all my problems. Still surprising how many it does fix. It's slightly embarrassing that I had to go to stackoverflow to be reminded to do it, but I found this answer useful.
@lukka sometimes visual studio is the underlying problem and restarting is the solution
For me, I had to install the xunit test runner that matched my xunit version. Then restart visual studio. Then it worked
J
JohnB

TLDR: Update the testing packages, look into the output -> test console

I struggled with this for a day and a half. so here's what I did to solve it:

Symptoms

5 Unit test projects, all discoverable in TestExplorer 2 out of 5 executed properly 3 stating not run at all The problem started after a .net framework update

Investigation

Since all the packages were updated during the .net framework update, I started with the differences between the working and not working projects. The first clue was that all 3 projects were using: MSTest.TestAdapter and MSTest.TestFramework

Naturally I went to the -> Output console -> Test dropdown in VS 2019 and looked at the output. Nothing useful there.

Step one of the solution: Update the MSTest.TestAdapter and MSTest.TestFramework to version 2.0.0

Step two of the solution: Now the Output console -> Test dropdown output started showing one after the other, missing packages and wrong binding redirects

Step three of the solution: Manually add the missing packages. For me those were

System.Runtime.Extentions System.Reflection Maybe some more that I'm missing

Step 4 of the solution: Removed/Fixed the unnecessary binding redirects.

I hope this will help someone else.


You saved me that day and a half! I upgraded a 4.5 FW version project and tried removing all unnecessary stuff from packages and ended up in this situation. Thanks!
I think I was experiencing something similar. Eventually I just re-created the project from scratch. Apparently there is a line in the *.csproj file that sets ToolsVersion and bumping mine messed everything up. All I can say is "buyer beware" for MS unit testing and be ready to replace your *.csproj files at a moment's notice this stuff is tightly integrated and very version dependent.
Thanks, while not directly my problem, the tip to check the console output showed me the error (DBContext Dispose method had an error in it) that was preventing my tests from running.
Nice. I already had MS Test 2.x, but updating the test packages to the latest version (2.2.7) still fixed it for me.
B
BlueRaja - Danny Pflughoeft

You can view the error-output of your test runner by opening the Output panel (view-->output) and choosing "tests" from the "Show output from" dropdown

https://i.imgur.com/C9RFrxx.png

Additionally, if you have Resharper installed you can open a test file and hover over the test-circle next to a test to get additional error info

https://i.imgur.com/QCEAdAu.png

Clicking that will bring you to a window with more detailed information. Alternatively, you can open that window by going to Extensions --> Reshaper --> Windows --> Unit Test Exploration Results

https://i.imgur.com/Tqp9y7n.png


The Test Output Log helped me to find an Exception deep down in Reactive Call Stack cause I had no mock for an Observable. Testing with Rx is quite challenging sometimes. But worth nonetheless.
This is a good way to troubleshoot the problem. In my case it was because... 'Method ClassCleanup has wrong signature'.
b
benmccallum

Check what framework the tests are written against (e.g. nunit, xunit, VS test, etc.) and make sure you've got the correct test adapter/runner extension installed.

For me it was NUnit 3 Test Adapter that was missing and I confirmed the version number required by looking at the nunit.framework dependency version (select the .dll in the Dependencies tree in Solution Explorer and hit F4 to bring up the Properties window).


Note to self: My original MSTest proj ref'd Microsoft.VisualStudio.QualityTools.UnitTestFramework (unsure which version). Resolved issues where method not run (or found) by removing that ref, and installing the latest (v2.1.0) MSTest.TestAdapter (and Framework)
@AdamCox Is Microsoft.VisualStudio.TestTools.UnitTesting deprecated? I had some tests using this that suddenly quit running as above after re-adding some references to a project they depended on.
@ebyrob, Athanasios' answer below might be relevant. Seems he not only had to upgrade/upgrade to the MSTest.* packages, but also add some dependencies and fix up some binding redirects. Check your warnings output.
m
markdotnet

I found that in the project it was not referencing the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly. Instead, it was referencing Microsoft.VisualStudio.TestPlatform.TestFramework and Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions. When I removed those two references and added the reference to the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly the tests that were previously marked with the blue exclamation point suddenly became active and started working.

https://i.stack.imgur.com/Q5psa.png

https://i.stack.imgur.com/6Ydb1.png


After switching from .NET Framework 4.6.2 to .NET Framework 4.7.2 my tests weren't running anymore. After hours of trying, only this solution worked for me.
After switching from .NET Framework 4.6.2 to .NET Framework 4.7.2 - I had to delete all the binding redirects in app.config
This fixed it for me as well! I also kept trying and realized from this post that Microsoft seems to be adding the wrong reference to the project when you create an MSTest UnitTest and its not recognized when you run in Visual Studio. Thanks so much for figturing this out!
This worked for me, a Great solution.. Our tests where just doing nothng. We removed those 2 and included the one and it worked perfectly.
d
double-beep

Clean-Rebuild solution worked for me.


J
Jon Ryan

I had this issue and for me it was caused by having multiple Test Projects with different versions of :

MSTest.TestAdapter

MSTest.TestFramework

Consolidating the nuget packages for the projects so they were the same resolved the issue for me.


This answer helped me find a similar cause. In my case some test projects had references to Microsoft.VisualStudio.TestPlatform.TestFramework (and did not run) while others had references to Microsoft.VisualStudio.QualaityTools.UnitTestFramework (which were the projects that would run).
A
AvishalomJan

Install Nunit3TestAdapter Nuget has solved this problem


this fixed my issue as well! :)
V
Vadim Leb

Had same issue after clean install of VS 2019. Tests are found but not run with "Unexpected error occurred". Fixed by setting up x64 instead of x86 which was selected by default.

https://i.stack.imgur.com/jMWMd.png


This was the fix for me, along with restarting VS and deleting the .vs folder. Thank you.
I can't believe that this is still a problem... Hopefully, Microsoft with output some kind of warning for that classic snag at one point.
S
SteveC

In my case, it was because one test project in my solution had the MSTest.TestFramework and MSTest.TestAdapter nuget packages installed but the others did not. These packages were apparently not required to run tests until one project in the solution had them installed.

The issue was fixed by installing those packages on test projects missing them.


In my case it's the exact opposite: Any unit test project, that has the MSTest nuget packages will not run, where as the others do. Strange.
Solved: I have a .testsettings file. MSTest packages require a .runsettings file. That's why the tests aren't running.
u
user2945722

What fixed it for me was upgrading the MS.Test nuget packages


Yes, for example when upgrading to .NET 6; everything will still compile, but those packages need to be upgraded too
A
Ali Besharati

In my case I had an async void Method and I replaced with async Task ,so the test run as i expected :

    [TestMethod]
    public async  void SendTest(){}

replace with :

    [TestMethod]
    public async  Task SendTest(){}

It's 2022 and I still had this problem. Why can't VS identify this problem and tell us? :/
v
volody

Have tried many options with Visual Studio 2019 Version 16.4.6 and Microsoft.VisualStudio.TestTools.UnitTesting, but for now the only way to run tests successfully was by invoking next command in console

dotnet test

Tests are discovered in Test Explorer but outcome is "Not Run".

Updating Visual Studio did not help.

Have resolved issue with "No test matches the given testcase filter FullyQualifiedName" by running updates to latest version for next packages:

Microsoft.NET.Test.Sdk
MSTest.TestAdapter
MSTest.TestFramework

Yes! This was my problem. Thank you! I am using xUnit. I upgraded - + And it started running my tests.
d
dotnet-provoke

https://i.stack.imgur.com/rZCHh.png

Setting Processor Architecture for AnyCPU Projects in Test Explorer fixed my issue! See screenshot above.


r
reZach

I am using XUnit and was missing the xunit.runner.visualstudio package. Installing it and my tests ran.


Exactly the same situation I had issued. Installing xunit.runner.visualstudio fix the problem. My unit tests use xUnit, but Visual Studio said that nUnit Test Adapter could'nt find with that name. So installing this package solved it.
K
Kristoffer la Cour

Had the same symptoms, in my case it was the dotnet core SDK version that was the problem.

The projects was targeting 2.2, and was able to build using 3.0. After installing newest 2.2 SDK version they were able to run.


s
sierra

What works for me is to delete the bin folder, then rebuild the project.


G
Gina

I had same symptoms.

Please ensure you have the proper Visual Studio extension installed via Tools - Extensions and Updates. In my case, I had to install XUnit and Specflow from the Online option.

Then clean the solution and rebuild it.

If that still doesn't help, clear your temp directory (search for %temp% in the Start menu search and delete all contents in Temp)

And then finally try uninstalling Resharper which finally fixed my problem.


Uninstall Resharper?! I don't think that this is a solution^^
C
CountOren

Here it was the test project was not marked to be built:

Build -> Configuration Manager... -> check build for your test project


K
Krzysztof Madej

I had different version of NUnit (3.11.0) and NunitTestAdapter (3.12.0) nuget packages. When I updated NUnit to 3.12.0, Visual Studio ran tests.


C
CodingYourLife

In my case it worked to update the MSTest nuget packages. Could reproduce this problem even on blank MSTest project and updating the packages worked.


I can confirm this works. At least for me. My tests were literally all working and then mid development they stopped and would not start. I tried several of the suggestions above: nothing. updating all the test packages restored function for me.
T
Thomas C. G. de Vilhena

This issue is also observed when the test method being run throws a StackOverflowException, making the test runner abort the test run, resulting in the output 0 tests run.

To find the culprit and solve it put a break point at the start of both TestInitialize and TestMethod decorated methods, run the unit test in debug mode, proceed stepping over (F10) until the exception is thrown.


I had the same issue. I am in the middle of a code refactor and just a few tests were aborting due to this exception yet 160 of my 316 tests would not run at all. Further, if you run the nunit tests via the command line in PowerShell you'll see the StackOverFlow exception and can use that output to work on the debug
J
James L.

I had a slightly different scenario, but this is the top Google result so I'll answer my issue too. I could run the tests but only about half of them actually ran, with no error output.

Eventually I debugged and stepped through all the tests, and came upon a Stack Overflow Error when creating Test Instances of some models.

In the test instance methods, a child model created a parent and the parent created a child, which created an infinite loop. I removed the parent creation from the child, and now all my tests work!


Thanks; I had a similar error; I'd mistyped a Parameter's name in the code I was testing... The test should have failed reporting that error; instead it showed as just not running.
v
vali

I changed "Debug" to "Release" and it worked again (Any CPU.)


I had the opposite work for me. Changed from release to debug and got my green ticks
K
Katelyn Rochat

https://i.stack.imgur.com/lJo2Z.png

https://i.stack.imgur.com/NHvID.png


f
felixh

I managed to figure out the reason for VS 2019/2022 skipping some tests by running the test cli with blame option:

vstest.console.exe myproject-test.dll /blame

This will generate an xml file with one test with "Completed="False".

In my case, the test host process crashed due to a memory access violation while running that test. The memory access violation was caused by an infinite recursion. You can right-click and debug that certain test.


p
profimedica

When using a mix of MSTests and NUnit tests it is possible to mix them. Also, remove other testing frameworks NuGet packages. Remove, Clean solution, Rebuild

This is the case:

[TestFixture]
public class NUnitTestClass
{
    [TestMethod]
    public void MSTestMethod()
    {
         // Will detect it but it won't run
    }
}

Adding the [TestFixture] attribute solved my problem