John V
John V

Reputation: 5047

Better example of incorrect statement coverage meaning?

I am trying to demonstrate that 100% statement coverage does not mean much in terms of proving and testing. I cannot think of any easy example, this is my best attempt:

TestObject t = null;

            if (Console.ReadLine() == "A")
            {
                t = new TestObject();
                t.Value = 5;

            }
            Console.WriteLine(t.Value);  //exception only when the IF statement was not run as the variable remains null.

Also when user presses "A", the statement coverage is 100%. However when anything else is pressed, the exception occurs.

Upvotes: 0

Views: 288

Answers (3)

Jose Luis Rivero
Jose Luis Rivero

Reputation: 271

Steve Cornnett, from Bullseye test coverage tool, has a fantastic article called "What is wrong with Statement Coverage?"

You will find a complete analisys on why statement coverage is consider as the weakest form of coverage.

Upvotes: 0

KhDonen
KhDonen

Reputation: 289

I would say your example is the good for that purpose.

  • You have complete (100%) statement coverage with just one test case. The bug was not revealed.
  • From the coverage point of view, there are two branches (IF evaluates TRUE or FALSE) and only one is covered (50%). If branch coverage reaches 100%, the bug will be exposed. This is called branch coverage.

Upvotes: 1

Malcolm O'Hare
Malcolm O'Hare

Reputation: 4999

I think you are incorrect in your assertion. With 100% statement coverage you can be sure that you are able to devise test cases to verify ALL your code. You seem to be confusing the idea that because you are able to hit every line in your code using a unit test that it is being fully tested with one test case.

Upvotes: 0

Related Questions