Build/Test Issues

Test fails in Nunit 3.10 but not 3.9

Started by GreenMoose on 3,698 views

[v3.15.0.6]
Below fixture fails with NUnit 3.10.1 but passes with NUnit 3.9.0, NCrunch or NUnit issue?

    [TestFixture]
    internal class ExceptionCatchFixture
    {
        [Test]
        public void CatchExceptionDoesNotFailTheTest()
        {
            try
            {
                Assert.IsTrue(false);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught: {0}", e.Message);
            }
        }
    }
This appears to be an intentional change in behaviour introduced in NUnit v3.10 - https://github.com/nunit/nunit/issues/2758.

Charlie's comments make sense. Trying to catch (and therefore 'ignore') the result of an assertion doesn't seem to make much sense from a logical standpoint, though I accept that there may be very niche scenarios where people might have a reason to do this.

In terms of implementation, what has changed here is that the assertion system under NUnit has become more sophisticated. Instead of just kicking up exceptions, the assertions now record the failures and pass them to the test runner on completion of the test as though the exceptions were never caught. Probably this was implemented because the NUnit developers now have a mechanism within which they can do it (through the multiple assertion blocks).

Here's what this means: You can now catch assertion exceptions to allow the test to continue to run, and the exceptions will still be reported with a failed test at the end of the run. So this is more powerful than it was before.

If you need the old behaviour, you can just avoid using the NUnit asserts and place an 'if' with a throw, or use a different assertion library (i.e. FluentAssertions).
I contributed in the NUnit discussion with my issue (testing custom assertion methods), but I guess throwing from another assertion library instead of NUnit is an option as well, thanks.

*Edit: Below suggestion worked out fine for me.

Te simplest approach is to use Assert.Throws to catch the exception rather than doing it yourself. Assert.Throws understands NUnit internals and ensures that the failure is not reported.

Edited

Post a reply

Log in to reply.