【问题标题】:Why I obtain this "NUnit.Framework.SuccessException" trying to debug a NUNIT test?为什么我在尝试调试 NUNIT 测试时获得此“NUnit.Framework.SuccessException”?
【发布时间】:2018-12-28 11:06:25
【问题描述】:

我是 NUNIT 的新手,我正在经历以下奇怪的行为。

在我的测试项目中,我有这个测试类:

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

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

    }
}

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

如果我运行此测试,它可以正常工作并且测试通过(Visual Studio 测试资源管理器中的绿色条)。

但是如果我尝试调试此方法(在 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

无论如何,测试似乎通过了。但是为什么我在调试模式下会得到这个异常呢?

【问题讨论】:

    标签: c# .net unit-testing nunit


    【解决方案1】:

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

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

    来自documentation...

    由于它会导致抛出异常,因此简单地让测试返回更有效

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 2014-08-14
        • 2023-03-15
        • 1970-01-01
        • 2012-04-01
        • 1970-01-01
        相关资源
        最近更新 更多