AndreaNobili
AndreaNobili

Reputation: 43077

Why I obtain this "NUnit.Framework.SuccessException" trying to debug a NUNIT test?

I am pretty new in NUNIT and I am experiencing the following stranhge behavior.

Into my test project I have this test class:

namespace Tests
{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void Test1()
        {
            Assert.Pass("PASSED");
        }

    }
}

that at the moment contain only the Test1() method that should pass every time that it is executed (because I am using the Assert.Pass() method.

If I run this test it works fine and the test pass (green bar inside Visual Studio Test Explorer).

But if I try to debug this method (putting a breackpoint on the Assert.Pass("PASSED"); line it give me this exception trying to executed the Pass() method:

Exception thrown: 'NUnit.Framework.SuccessException' in nunit.framework.dll
An exception of type 'NUnit.Framework.SuccessException' occurred in nunit.framework.dll but was not handled in user code
PASSED

The test anyway seems to be passed. But why am I obtaining this exception in debug mode?

Upvotes: 3

Views: 4495

Answers (2)

Rev
Rev

Reputation: 57

It's always a good idea to read documentation on the method used:

/// <summary>
/// Throws a <see cref="SuccessException"/> with the message and arguments
/// that are passed in. This allows a test to be cut short, with a result
/// of success returned to NUnit.
/// </summary>

https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Assert.cs#L105

Upvotes: 2

Fenton
Fenton

Reputation: 251252

Assert.Pass does indeed throw an exception, which allows you to send a message to the test runner. You very rarely need to use an Assert.Pass as you don't need to do this for the test to pass. You can instead allow the test to complete.

Obviously, your real tests should assert something, but usually you'll be checking a value is as expected. When it is, there will be no exception.

From the documentation...

Since it causes an exception to be thrown, it is more efficient to simply allow the test to return

Upvotes: 7

Related Questions