Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 781
lysergic-acid Avatar asked Jun 16 '11 13:06

lysergic-acid


2 Answers

The Assert.DoesNotThrow is redundant, if a tests throws the test will fail automatically without an assert. To clearify what is beeing 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.

like image 173
vidstige Avatar answered Sep 20 '22 20:09

vidstige


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.

like image 3
skrebbel Avatar answered Sep 19 '22 20:09

skrebbel