posted on Wednesday, June 07, 2006 4:19 AM by bben

Tricky thing about unit testing

Hello,
Here is a tricky thing that seems obvious to me once I've seen it but which has suprised me the first time I've faced it.

Simple class:
public class Foo
{
   public string Do(bool b)
   {
      string s = null;
      if (b)
         s = "net";

      return s.ToUpper();
   }
}

Simple test class with NUnit:
[TestFixture]
public class FooTest
{
   [Test]
   public void DoTest()
   {
      Foo f = new Foo();
      string  s = f.Do(true);
      Assert.IsTrue(s.Equals("NET"), "NOT EQQQQQUAL!!!");
   }
}

If you launch this test (for exemple with TestDriven.NET and VS2003), you will get : "Test passed, method Foo has a test coverage rate at 100%".
But, if you execute method Do with false as parameter, you'll get an Exception.

[Updated]
So having successfull tests and a test coverage at 100% for a method does not mean that this method is free of bugs !
Having your tools telling you that all your tests are successfull and that your tests cover 100% of your method code does not mean that this method is free of bugs.

Comments