Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Test fails in Nunit 3.10 but not 3.9
GreenMoose
#1 Posted : Wednesday, April 25, 2018 3:53:54 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 507

Thanks: 145 times
Was thanked: 66 time(s) in 64 post(s)
[v3.15.0.6]
Below fixture fails with NUnit 3.10.1 but passes with NUnit 3.9.0, NCrunch or NUnit issue?
Code:

    [TestFixture]
    internal class ExceptionCatchFixture
    {
        [Test]
        public void CatchExceptionDoesNotFailTheTest()
        {
            try
            {
                Assert.IsTrue(false);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught: {0}", e.Message);
            }
        }
    }
Remco
#2 Posted : Wednesday, April 25, 2018 11:57:05 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
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).
1 user thanked Remco for this useful post.
GreenMoose on 4/26/2018(UTC)
GreenMoose
#3 Posted : Thursday, April 26, 2018 6:56:35 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 507

Thanks: 145 times
Was thanked: 66 time(s) in 64 post(s)
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.
Quote:

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.
1 user thanked GreenMoose for this useful post.
Remco on 4/26/2018(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.026 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download