Nunit SuccessException marks test as failed

Mar 12, 2013 at 12:31 PM
Hi! I'm using StatLight in combination with nunit-silverlight (for SL5 projects) and I'm running test both from console and from AgUnit plugin for Resharper. What happens is that if I have Assert.Pass() in my unit test (which boils down to 'throw new SuccessException(string.Empty)`) my tests are marked as failed both from AgUnit and in the console. What I get is this:
------------------ Test Failed ------------------
Test Namespace:    My.Tests.Example
Test Class:        ExampleClassTests
Test Method:       ExampleTest
Exception Message:
   at NUnit.Framework.Assert.Pass(String message, Object[] args)
   at NUnit.Framework.Assert.Pass()
   at My.Tests.Example.ExampleClassTests.ExampleTest()
Is this intentional? Is there a way to work around it? Thanks!
Mar 12, 2013 at 8:01 PM
I found your report interesting and had to actually download the full NUnit source to see if they also threw a SuccessException. I had a hard time believing they actually threw an exception for a 'passed' scenario.

I'm curious what scenario you are running where you would prefer to Assert.Pass vs just allow the method to return w/out an exception?

You have a couple approaches to work around this.
  1. (Recommended) Don't use Assert.Pass - if the method doesn't throw an exception, then it is a passing method. If you need to stop your test find alternative approaches (return, for-loop-breaks, etc...)
  2. Create some Assert.Throws<SuccessException>(()=>{ ...code...}) wrapper, but that just sounds like a pain.
  3. (just a thought - but not recommended) try putting the [ExpectedException(typeof(SuccessException))] attribute on the test.
Personal Opinion: Throwing an exception for control-flow (especially of a passed test) goes against about everything I've ever seen in testing. The documentation says you should use this to 'cut-short' the execution of a test and while I agree that some tests could be cut-short I'm afraid throwing an exception is the last method I'd use for this.

Hope this helps, and thanks for bringing this to my attn.
Mar 13, 2013 at 9:34 AM
Thanks for the quick reply! I was mostly curious to see if this was intentional or if I was doing something wrong.

I rarely used Assert.Pass() and only when I wanted to make it more explicit that the test should reach that line. Kind of an opposite case to [ExpectedException]) when you put Assert.Fail() as a safety line after the method call that you expect to fail.

Thanks for the suggestions to work around this, I think I'll remove the statement completely, especially now after your explanation.