17

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.

Bogi
  • 2,274
  • 5
  • 26
  • 34

5 Answers5

18

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

Bogi
  • 2,274
  • 5
  • 26
  • 34
16

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

Vlad
  • 1,977
  • 19
  • 44
Darrel Hoffman
  • 4,436
  • 6
  • 29
  • 41
  • Good tip, but what if I want to break on System.Diagnostics.Debug.Assert while debugging a unit test? – yoyo Apr 14 '16 at 00:25
  • Found a solution - use [TestInitialize] to add a custom System.Diagnostics.TraceListener that throws a UnitTesting.AssertFailedException in the event of a Debug.Assert failure. – yoyo Apr 14 '16 at 17:35
  • @yoyo: _"what if I want to break on System.Diagnostics.Debug.Assert while debugging a unit test"_ -- calling that method with a value of `false` for the `condition` parameter will, according to the documentation, display a dialog providing three options: "Abort", "Retry", and "Ignore". Choosing "Retry" will break in the debugger (similar to calling `Debugger.Break()`). There's no need to involve exception handling at all; the `Assert()` method is completely unrelated to exception handling and breaking in the debugger for `Assert()` involves a completely different mechanism. – Peter Duniho Jul 07 '20 at 23:05
6

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.

vc 74
  • 37,131
  • 7
  • 73
  • 89
1

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.

VinayC
  • 47,395
  • 5
  • 59
  • 72
0

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
Sahil Singh
  • 3,352
  • 39
  • 62