четверг, 9 декабря 2010 г.

Reduction of test coverage and TDD.

I have been practicing TDD for a couple of months, when I realized, that sometimes I don't have 100% test coverage. I've started investigation of that phenomenon.

The type of coverage violation that presented a problem is:
  • Branch coverage.

When we write a method, which satisfies some certain test, we can add a number of if expressions. These if expressions are necessary in general case, but else branch is never executed within this set of tests. It produces reduction of branch coverage.

For example.

Problem:
Return only positive numbers in the array.

Solution:

List M(int[] a) {
result = new ArrayList();
for (int i : a) {
if (i >= 0) result.add(i);
}
return result;
}


And if we have only the following unit test:

a = int[]{1,2,3}
res = M(a)
assertEquals({1,2,3}, res)


After we run this test, the brunch coverage will be less than 100%. The 'else' clause is never executed. We understand that though there is no such test case with negative values 'if' clause is necessary in the method M.

We can easily add -1 to the array and satisfy the branch coverage, but unlike this simple example
production examples can be more complicated.

Should we write tests for all execution paths? Accordingly to TDD by example(Kent Beck) it is normal to use tests as specification, but will not this lead to overspecified software(xUnit test patterns, Gerard Meszaros)?

Комментариев нет:

Отправить комментарий