为什么我在尝试调试 NUNIT 测试时获得此 "NUnit.Framework.SuccessException"?

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

我是 NUNIT 的新手,我遇到了以下奇怪的行为。

进入我的测试项目我有这个测试class:

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

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

    }
}

目前只包含 Test1() 方法,每次执行时都应该通过(因为我使用的是 Assert.Pass() 方法。

如果我 运行 这个测试工作正常并且测试通过(Visual Studio Test Explorer 内的绿色条)。

但是如果我尝试调试此方法(在 Assert.Pass("PASSED"); 行上放置一个断点,它会给我这个异常来尝试执行了 Pass() 方法:

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

反正测试好像是通过了。但是为什么我在调试模式下会得到这个异常?

Assert.Pass 确实会抛出异常,它允许您向测试运行器发送消息。您很少需要使用Assert.Pass,因为您不需要为了通过测试而这样做。您可以让测试完成。

显然,您的实际测试应该断言 something,但通常您会检查一个值是否符合预期。到时候也不例外。

来自 documentation...

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

阅读有关所用方法的文档总是一个好主意:

/// <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