Assert.DoesNotThrow with NUnit -- how to get stack trace?
Asked Answered
D

2

9

I am using NUnit for unit testing (running with TD.NET).

When using Assert.DoesNotThrow, i get the exception, but without any stack trace, which makes it harder to identify root issue.

How can i tackle this?

Durra answered 16/6, 2011 at 13:28 Comment(1)
Just for info: Both Resharper unit test and Visual Studio unit test (using VSTest and the NUNit3Adapter) show full stack trace for the DoesNotThrow.Tris
H
12

The Assert.DoesNotThrow is redundant, if a tests throws the test will fail automatically without an assert. To clarify what is being tested is, in my humble opinion, better conveyed in the test method name. There is very little documentation benefit in the Assert.DoesNotThrow syntax and, as you point out, just makes it harder to fix failing tests.

Also, if you have a very long test with multiple asserts the Assert.DoesNotThrow fills an important role as to assert that the correct code block threw an exception. However, in this case a more suitable solution is to see if the test can be shortened and/or asserts moved into their own tests.

Heideheidegger answered 16/6, 2011 at 14:41 Comment(0)
B
3

I doubt it really answers your question, but I'd shorten the test to a single situation so that you don't need to wrap some code into a delegate that you pass to Assert.DoesNotThrow. Then, just write the test without any assertions. A test that throws an unexpected exception will fail, so it will do what you want, and you'll get the full exception.

About 10%-ish of my tests work like this; no assertions at all, and method names like ThisOrThatShouldNeverThrow().

Another option, while debugging, is to run the test in a debugger (using TD.Net) and in Debug|Exceptions check some additional boxes so that the debugger stops when the exception is thrown.

Bambibambie answered 16/6, 2011 at 14:26 Comment(2)
There are many other ways to accomplish this, but i was wondering about the DoesNotThrow method, and more specifically, if NUnit can be configured to display this information. I can mimic the DoesNotThrow method (which simply wraps a try..catch i assume) and output this information myself.Durra
Ok, I thought you were trying to solve a practical problem. In this case, why not patch it in NUnit and contribute it? Fair chance you're not the only person interested in this feature.Bambibambie

© 2022 - 2024 — McMap. All rights reserved.