Passes, Ignores, Fails and Warnings

When writing tests, you should always think in black and white. Or red and green if you the GUI test runner. All tests should pass and fail and that should be that.

Of course, there are times where shades of grey (or yellow in the GUI case) may appear because we want the test runner not to run all the tests in a fixture. It may take too long Alternatively, we may want to write a test that passes for now but leaves a warning that things will change in the future. MbUnit provides for these grey areas with the [Ignore] attribute for tests and Assert.Warning().

Flagging Tests To Be Ignored

To tell the MbUnit test runner to ignore a test during its run, simply adorn a test method with the [Ignore] attribute as follows.

      [Ignore]
      [TestCategory("Category1")]
      [Row(5)]
      [Row(10)]
      [RowTest]
      public void ToFizzBuzz_SendNumberDivisibleBy5ButNot3_ReturnsBuzz(int NumberToTest)
      {
         Assert.AreEqual("buzz", FizzBuzz.FizzBuzz.ToFizzBuzz(NumberToTest));
      }

If you’re using the GUI runner tests that were ignored will be indicated in yellow rather than red or green.

MbUnit GUI runner showing ignored tests. Click this thumbnail image to show full size image

If you’re using the console runner the ignored tests will be tallied and displayed in the output and any report you choose to generate.

Putting Warnings Into Your Tests

If ignores are yellow, then warnings are transparent. Tests should either pass or fail. However, a unit test might not fail but also not succeed entirely: making it fail is too extreme and on the other hand, letting him succeed would make you miss the issue. Therefore, MbUnit also supports warnings, generated as part of a test’s code using Assert.Warning(). For example,

      [Row(3)]
      [Row(6)]
      [RowTest]
      public void ToFizzBuzz_SendNumberDivisibleBy3ButNot5_ReturnsFizz(int NumberToTest)
      { 
         Assert.Warning("Values divisble by 15 shouldn't be tested here");
         Assert.AreEqual("fizz", FizzBuzz.FizzBuzz.ToFizzBuzz(NumberToTest));
      }

You can add as many warnings as you like to a single test, but bear in mind that warnings only appear in reports generated by MbUnit (in the Html report, they are displayed right after the fixture summary as shown below) and are not flagged up in the default output of either the console or GUI runner which only show if a test has passed, failed or been ignored.

MbUnit report showing test warnings. Click this thumbnail image to show full size image