I have this property:
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;
}
}
}
and this unit test
[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);
}
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?
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.
Edit — here’s the plugin code in its entirety. You’re on your own for building the actual plugin though — check the NUnit site 🙂