C# 为什么我在调试NUnit测试时获得了这个“NUnit.Framework.SuccessException”?

C# 为什么我在调试NUnit测试时获得了这个“NUnit.Framework.SuccessException”?,c#,.net,unit-testing,nunit,C#,.net,Unit Testing,Nunit,我在努尼特是个新手,我正在经历以下的stranhge行为 在我的测试项目中,我有以下测试类: namespace Tests { public class Tests { [SetUp] public void Setup() { } [Test] public void Test1() { Assert.Pass("PASSED");

我在努尼特是个新手,我正在经历以下的stranhge行为

在我的测试项目中,我有以下测试类:

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

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

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

如果我运行这个测试,它工作正常,并且测试通过VisualStudioTestExplorer中的绿色条

但是如果我试图调试这个方法,在Assert.PassPASSED上放置一个断点;第行:尝试执行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,因为您不需要这样做才能通过测试。您可以改为允许测试完成

显然,您真正的测试应该断言一些东西,但通常您会检查一个值是否符合预期。如果是这样,也不会有例外

因为它会导致抛出异常,所以更有效的方法是简单地允许测试返回

Assert.Pass确实会引发异常,这允许您向测试运行程序发送消息。您很少需要使用Assert.Pass,因为您不需要这样做才能通过测试。您可以改为允许测试完成

显然,您真正的测试应该断言一些东西,但通常您会检查一个值是否符合预期。如果是这样,也不会有例外

因为它会导致抛出异常,所以更有效的方法是简单地允许测试返回


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

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

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

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