问题描述
我具有此属性:
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在我显示消息框时失败运行测试,但允许它在我调试应用程序时向我显示?
Is there a way I can prevent Debug.Fail displaying a message box when I run my tests, but allow it to show me one when I debug my application?
推荐答案
我一直做的标准方法这是为NUnit创建一个插件。该插件仅取消注册默认的跟踪侦听器,并注册一个替换,该替换在触发Assert / Trace.Fail时引发异常。我喜欢这种方法,因为如果断言触发,测试仍然会失败,您不会弹出任何消息框,也不必修改生产代码。
The standard way I've always done this is to create a plugin for NUnit. The plugin simply unregisters the default trace listener and registers a replacement that throws an exception when Assert/Trace.Fail is triggered. I like this approach because tests will still fail if a assert trips, you don't get any message boxes popping up and you don't have to modify your production code.
编辑-这是完整的插件代码。不过,您可以自己构建实际的插件-检查NUnit网站:)
Edit -- here's the plugin code in its entirety. You're on your own for building the actual plugin though -- check the NUnit site :)
[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);
}
}
}
这篇关于我可以配置NUnit以便在运行测试时Debug.Fail不显示消息框吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!