Visual Studio "Debug Unit Test" fails to hit breakpoints
.NetVisual StudioDebuggingVisual Studio-2008Resharper.Net Problem Overview
When using Visual Studio 2008 and debugging my unit tests... sometimes the debugger works fine. However very often after modifying some code then clicking "Debug Unit Test" Visual Studio will fail to hit the breakpoints in the code. The debugger basically hangs and eventually the tests runs with the new code anyway, but never stops to let me see what is going on.
I'm assuming this has something to do with some type of assembly caching done by the debugger, but not matter what I do (clean project, delete bin folders, restart VS, etc) I can never get the right assembly to load. Has anyone else seen this behavior? Any solutions?
By the way, using Resharper 4.5, and .NET 3.5 on Win XP.
.Net Solutions
Solution 1 - .Net
I just had a problem hitting breakpoints in VS2015.
I am always using the solution configuration called Debug but for some reason my solution was set to build the Release version.
Switching from Release to Debug in the dropdown at the top of Visual Studio fixed my problem.
Solution 2 - .Net
Right click + Run Test(s) will not hit the breakpoint.
Right click + Debug Test(s) will!
Solution 3 - .Net
Another workaround: Force the debugger to be launched from within your unit test:
System.Diagnostics.Debugger.Launch();
Solution 4 - .Net
What happened to be the solution for me: make sure all your nuget package versions match. My Unit Test project was using a version of Newtonsoft.Json
that was newer than the Newtonsoft.Json
reference on the project I was testing. Once I updated all nuget packages to the latest version I was able to hit the breakpoint
Solution 5 - .Net
One problem that I stumbled upon when trying to debug a test method was that it was private. Simply changing the method from private
to public
fixed my problem.
I don't know why this is a problem, but it probably has something to do with the implementation of the [Test]
attribute of NUnit.
Solution 6 - .Net
Now we have this problem with Visual Studio 2017 15.5 and Resharper 2017.2. Problem caused by Resharper and solved in latest versions 2017.3+
Solution 7 - .Net
For me I went to Test explore -> setting -> processor Architecture For AnyCPUProjects and changed it to X64 and it worked for me.
Solution 8 - .Net
Very late in the day, but in case anyone else ends up here like I did....
My tests would not hit a breakpoint. I went to bare bones in the test (below) but still not hitting:
[Fact] // xunit, c#, vs 16.9.0
public void TestX()
{
var messages = new List<int>();
Assert.NotNull(messages);
}
Copied the same test to a different project, hit the breakpoint straight away.
Tried a few things, then spotted the problem...
In my original test project, I referenced the SUT project, which had the following SDK package reference copied from the SUT:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
Once I removed this on my test project, breakpoints were being hit again!
Frustrating and wasted time that I didn't have, so hopefully helps someone else!
Solution 9 - .Net
I had the same problem, although I don't have permanent solution, this is a quick one time fix: Debug the unit test (Ctrl-T, Ctrl-D), then go to "Immediate Window", enter anything (e.g. 'a' or null) and press enter. After this the break point will be hit.
Solution 10 - .Net
The breakpoint is not hit when starting debugging from the "Unit Test Sessions" window (Resharper - Windows - Unit Test Sessions) which comes from ReSharper.
But when starting the test from the "Test Explorer" window (Test - Windows - Test Explorer) of VS it hits the breakpoint.
VS Enterprise 2017 V15.5.6, ReSharper 2017.2.2
The latest ReSharper 2017.3.1 is not an option because it has other bugs
Solution 11 - .Net
Ensure [TestMethod] attribute is present for the method, [TestClass] is present for class.
Solution 12 - .Net
If you have [HostType("ASP.NET")]
, remove it and Test -> Debug -> Run your tests again
Solution 13 - .Net
Make sure you are debugging the correct test!
I have some tests with very similar names except for the last word in the test name. I had the break point set in the first test and was using Visual Studios "Test Explorer" window to "Debug Selected Tests" on the second test, which didn't have a breakpoint set.
Test names
PublishAsync_Valid_Acked
PublishAsync_Valid_Nacked
Solution 14 - .Net
If you are using a setup method with [TestInitialize]
\ [ClassInitialize]
attribute (mstest \ nunit)? try to check if that setup code is being executed successfully.
For example:
[TestInitialize]
public void Setup()
{
throw new Exception();
}
[TestMethod]
public void SomeFooTest()
{
//The breakpoint will "Fail" to hit here.
}
In visual studio you can easily see this behavior with Test Explorer or by the CodeLens (only in professional):
Solution 15 - .Net
Reference the nuget package xunit.runner.visualstudio into your test project.
I had the same scenario, none of the above mentioned suggestions worked for me.Then I referenced a nuget package xunit.runner.visualstudio and the issue is solved
Solution 16 - .Net
I got another possible solution - Check for exceptions thrown "silently".
I'm using VS 2019 Professional. I opened Test Explorer, clicked my desired test and chosen "Debug" (had a breakpoint in the test itself). The breakpoint was not hit. I kept looking for answers in the "Tests" output window, but there was only info, that the test has run and finished (failed).
Then I discovered that clicking on the single test in Test Explorer and looking down, there's "Test Detail summary" and voila. The message there said, there's an exception thrown inside the test. I fixed the problem causing this exception and it started hitting. The tricky thing was, that I didn't know about the exception, there was no other notification than the one in Test detail summary.