Cook Computing

NUnit Ignore Attribute

January 24, 2003 Written by Charles Cook

NUnit has a nice feature for specifying that test fixtures or individual tests should be ignored: the Ignore attribute. For example:

[Ignore("code for handling different cultures not implemented")]
public void testXmlRpcDouble_ForeignCulture()
  CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
  XmlDocument xdoc;
    Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
    XmlRpcDouble xsd = new XmlRpcDouble(543.21);
    xdoc = Utils.Serialize(
      new XmlRpcDouble(543.21), 
      Encoding.UTF8, MappingAction.Ignore);
    Thread.CurrentThread.CurrentCulture = currentCulture;
  Type parsedType, parsedArrayType;
  object obj = Utils.Parse(xdoc, null, MappingAction.Error, 
    out parsedType, out parsedArrayType);
  Assertion.AssertEquals(543.21, obj);

Its very convenient when you think of something that needs testing but which cannot be tested yet for some reason. A bit like making a note in your project todo list except in this case you won't be able to ignore or lose the note: each time you run your tests in the NUnit gui the ignored tests and the overall progress/success bar are marked in yellow instead of the usual green for sucess. This is much better than commenting out code in a test harness where it is likely to be forgotten.