Assert.Throws doesn't show stacktrace if assert fails

506 views
Skip to first unread message

jouke.numan

unread,
Jul 15, 2009, 5:30:57 AM7/15/09
to NUnit-Discuss
When Assert.Throws fails the assertion, it only prints the name of the
exception that occurred.

If the ExpectedException attribute is used, a stacktrace is printed
from which you can directly jump to any line in that trace. Often this
is enough to track the problem, if not I can then easily set
breakpoints, rerun in debug mode and diagnose the problem.

Is there a way to get the stack trace printed when the assertion
fails?

Now I have to either lift the code to test from the assert and rerun
or configure the debugger to break when the specific exception is
thrown and rerun the test.

Charlie Poole

unread,
Jul 15, 2009, 1:32:30 PM7/15/09
to nunit-...@googlegroups.com
This is an artifact of a particular feature of NUnit,
which affects more than just ThrowsConstraint. So we should
discuss here and decide whether to change it only for
ThrowsConstraint or more generally.

NUnit distinguishes between unexpected exceptions - called
"Errors" - and assert failures - known as "Failures."

Sometimes the handling of the two is subtly different and
this is one case. For "Errors" the stack trace shows all
nested inner exceptions for "Failures" we only show the
trace up to the point of the test itself.

It seems to me that we could safely shoow nested exceptions
in both cases: for Assert failures, the inner exception
would normally be null. The main issue is how to tell
NUnit that there is an inner exception. The exception
is not generated by the constraint, but by the assertion
handling the failure. I suppose we could add an exception
field to the constraint, which would be queried by
Assert.That in order to use it as the inner exception
for the assertion exception.

For NUnit 3.0, I think we need a revision of how this
is handled. For NUnit 2.5.x, I'm willing to do it if
we can come up with a relatively simple approach.

Charlie
Reply all
Reply to author
Forward
0 new messages