Break the debugger on assertion failed

12.5k views Asked by At

Is there a way to break the debugger when assertion is false and running the application using Visual Studio debugger. Earlier when I was debugging Windows application I would get an exception and the debugger would break, but now on Smart Device an assertion failed window is showed with stack trace, but I would also like to see variable values etc.

5

There are 5 answers

4
Bogi On BEST ANSWER

Stupid me, the solution was simple. When the window pops out, press pause in debugger :)

0
VinayC On

It seems that you can attach the Debugger when assertion fails to see other details - see this article: http://blogs.msdn.com/b/davidklinems/archive/2005/08/29/457847.aspx. Its quite dated but perhaps still applicable.

0
vc 74 On

In addition to Vinay's solution, you can start the debugger for a specific process by calling

Debugger.Break

In your case you could do it every time the listener receives a failure message.

3
Darrel Hoffman On

Not sure about VS 2008, but in at least 2010 and later, you can go to Debug/Exceptions (Ctrl-Alt-E). Click the "Add" button, choose "Common Language Runtime Exceptions", and type:

Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException

and hit "OK". It will now appear on the list, make sure you check the checkbox in the "Thrown" column. You will now get a break on any assert failure.

Updated: screenshot from VS 2017 AssertFailedException

0
Sahil Singh On

For a Native Unit Test project (C++), we can follow a method similar to the one in @Darrel Hoffman's answer.

Go to Debug->Windows->Exception Settings.

Debug->Windows->Exception Settings

Add a new exception under Win32 Exceptions.

Add a new exception under Win32 Exceptions

For error code enter 0xE3530001, and give it some description.

For error code enter 0xE3530001, and give it some description

For error code enter 0xE3530001, and give it some description Now, from test explorer, select Debug, instead of Run.

from test explorer, select Debug, instead of Run

Alternatively, it will work even if you select <All Win32 Exceptions not in this list>.

Alternatively, it will work even if you select All Win32 Exceptions not in this list

Visual Studio will now break whenever a Native Unit Test assertion (Assert::IsTrue, Assert::IsFalse, etc.) fails.

Note that by default breaking on 0xc0000420 Assertion failed exception is enabled in Visual Studio exception settings, however it doesn't cause the debugger to break when our unit test assertion fails, hence the steps above are necessary.

by default breaking on Assertion failed exception is enabled in Visual Studio exception settings, however it doesn't cause the debugger to break when our unit test assertion fails, hence the steps above are necessary

PS: @Darrel Hoffman' answer is sufficient for C# unit tests, but for native C++ a few extra steps are required, hence my answer may help those like me who stumble upon this for native use case.

References:

  1. https://unparalleledadventure.com/2019/02/05/the-one-where-we-reverse-engineered-microsofts-c-unit-test-framework-part-3-exceptions/
  2. https://stackoverflow.com/a/27660893/981766