我可以配置NUnit,以便在运行测试时Debug.Fail不显示消息框吗?

时间:2010-04-18 11:20:48

标签: c# debugging nunit

我有这个属性:

    public SubjectStatus Status
    {
        get { return status; }
        set
        {
            if (Enum.IsDefined(typeof(SubjectStatus), value))
            {
                status = value;
            }
            else
            {
                Debug.Fail("Error setting Subject.Status", "There is no SubjectStatus enum constant defined for that value.");
                return;
            }
        }
    }

和本单元测试

    [Test]
    public void StatusProperty_StatusAssignedValueWithoutEnumDefinition_StatusUnchanged()
    {
        Subject subject = new TestSubjectImp("1");

        //  assigned by casting from an int to a defined value
        subject.Status = (SubjectStatus)2;
        Assert.AreEqual(SubjectStatus.Completed, subject.Status);            

        //  assigned by casting from an int to an undefined value
        subject.Status = (SubjectStatus)100;
        //  no change to previous value
        Assert.AreEqual(SubjectStatus.Completed, subject.Status);            
    }

有没有办法可以阻止Debug.Fail在运行测试时显示一个消息框,但是当我调试我的应用程序时允许它显示一个消息框?

3 个答案:

答案 0 :(得分:8)

不需要更改生产代码或编写自定义NUnit加载项的替代方法是替换安装夹具中的跟踪侦听器。

E.g。在测试所在的命名空间中添加以下类:

using System;
using System.Diagnostics;
using NUnit.Framework;

[SetUpFixture]
public class NUnitSetup
{
    // Field to hold exisitng trace listeners so they can be restored after test are run.
    private TraceListener[] originalListeners = null;

    // A trace listener to use during testing.
    private TraceListener nunitListener = new NUnitListener();

    [SetUp]
    public void SetUp()
    {
        // Replace existing listeners with listener for testing.
        this.originalListeners = new TraceListener[Trace.Listeners.Count];
        Trace.Listeners.CopyTo(this.originalListeners, 0);
        Trace.Listeners.Clear();
        Trace.Listeners.Add(this.nunitListener);
    }

    [TearDown]
    public void TearDown()
    {
        // Restore original trace listeners.
        Trace.Listeners.Remove(this.nunitListener);
        Trace.Listeners.AddRange(this.originalListeners);
    }

    public class NUnitListener : DefaultTraceListener
    {
        public override void Fail(string message)
        {
            Console.WriteLine("Ignoring Debug.Fail(\"{0}\")", message);
        }

        public override void Fail(string message, string detailMessage)
        {
            Console.WriteLine("Ignoring Debug.Fail(\"{0},{1}\")", message, detailMessage);
        }
    }
}

答案 1 :(得分:2)

我一直这样做的标准方法是为NUnit创建一个插件。该插件只是取消注册默认跟踪侦听器,并注册一个在触发Assert / Trace.Fail时抛出异常的替换。我喜欢这种方法,因为如果断言跳闸,测试仍然会失败,您没有弹出任何消息框,也不必修改生产代码。

编辑 - 这是完整的插件代码。您可以自己构建实际的插件 - 查看NUnit站点:)

[NUnitAddin]
public class NUnitAssertionHandler : IAddin
{
    public bool Install(IExtensionHost host)
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new AssertFailTraceListener());
        return true;
    }

    private class AssertFailTraceListener : DefaultTraceListener
    {
        public override void Fail(string message, string detailMessage)
        {
            Assert.Fail("Assertion failure: " + message);
        }

        public override void Fail(string message)
        {
            Assert.Fail("Assertion failure: " + message);
        }
    }
}

答案 2 :(得分:0)

不是直接调用Debug.Assert,而是调用一个包装器方法,在调用Debug.Assert之前检查是否附加了调试器。 (据推测,如果没有附加调试器,它应该抛出异常,这样你的测试就会失败。)例如:

[Conditional("DEBUG")] 
public static void Assert(bool condition) 
{ 
    if (Debugger.IsAttached) 
    { 
        Debug.Assert(condition); 
    } 
    else 
    { 
        throw new SomeException(); 
    } 
}