Clever way to make code continue following a test failure?

1,544 views
Skip to first unread message

Tom Evans

unread,
Nov 26, 2015, 12:56:57 PM11/26/15
to NUnit-Discuss
Hello,

I'm new to NUnit.  I'm using version 2.6.4 with C#.  So far, I'm finding NUnit to be a really useful and slick product.

I have some existing test code that runs in STAF/STAX, which uses XML and Python. I'm trying to translate this existing test code into C#, and run it from NUnit.  I realise that the fundamental principle of NUnit is that, if an assertion fails, all subsequent Asserts will not be executed.

My problem is that I'd like to run test sequences where test program flow continues if an Assert fails.  My existing code calls an 'Evaluate' function which reads in a boolean, reports test pass/fail, and then continues through the code regardless.  Ideally, I'd like to keep the same programming constructs that have already been written for STAF/STAX, then my conversion job is much easier.  If I can invent an equivalent 'Evaluate' function, I can simply translate the python sequences/loops/if-else constructs into the C# equivalent.

So, can anyone please suggest a way that I can replace my Evaluate function with something in C#?

Thanks in advance.
Tom

Charlie Poole

unread,
Nov 27, 2015, 10:18:02 AM11/27/15
to NUnit-Discuss
Hi Tom,

As you said, Asserts in NUnit do stop execution. The unit of execution
in NUnit is the test, so if you want to ensure that several Asserts
execute no matter what, they need to be in separate tests. However, if
they are in separate tests you lose the ability to control their
order.

We have plans for loosening both of these restrictions. There is a
request for multiple asserts and several having to do with test
ordering and dependencies. You won't see any work on those changes for
several releases, however.

Charlie

Charlie
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nunit-discus...@googlegroups.com.
> To post to this group, send email to nunit-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/nunit-discuss.
> For more options, visit https://groups.google.com/d/optout.

Brad Stiles

unread,
Feb 25, 2016, 11:49:55 AM2/25/16
to nunit-...@googlegroups.com
Perusing old threads and saw this.  Had to do something similar at one point.  Could probably do something cleverer with TestContext or some other form of delegate, but this sufficed for my simple case.

private static string TryAssert(Action action)
{
    try {
        action();
        return string.Empty;
    }
    catch (AssertionException e) {
        Console.WriteLine(e);
        return e.ToString();
    }
}
 
[Test]
public void VerifySomeStuff()
{
    string result = string.Empty;
    result += TryAssert(() => Assert.That(1, Is.EqualTo(2)));
    result += TryAssert(() => Assert.That(2, Is.EqualTo(2)));
 
    Assert.That(result, Is.Null, result);
}

Charlie Poole

unread,
Feb 25, 2016, 12:09:54 PM2/25/16
to NUnit-Discuss
Note to everyone: 99.9% of the time, don't do this! :-)

Seriously, whether it's NUnit or some other framework, learn to work
with rather than against it's grain. If you find yourself having to
"trick" the framework quite regularly, you either have the wrong
framework or you don't understand how to use it!

That said, Brad, it's a good trick to simulate multiple Asserts, a
feature that's on our backlog but not yet started.

Charlie
> Visit this group at https://groups.google.com/group/nunit-discuss.

Brad Stiles

unread,
Feb 26, 2016, 1:22:21 PM2/26/16
to nunit-...@googlegroups.com
Note to everyone: I agree with Charlie 100% on this one. :)

To be clear, this quickly solved a problem I had. It was more of an integration test, and not a "unit" test in the "proper" sense.

It was a data promotion process, and *designed* for following steps to sometimes proceed when previous steps failed.
Reply all
Reply to author
Forward
0 new messages