Rob Smyth

Wednesday, 3 June 2009

Cyclomatic Complexity To Monitor Unit Test Coverage

NCoverCop has proven to be a great tool that I like a lot. It fails the build if code coverage falls. But, people always trump process and there is always the temptation to write test cases for code coverage rather than test the code. All too easy. I wonder if a better approach would be to measure project cyclomatic complexity (CC) against number of test cases.

If the code CC rises then the number of unit tests is expected to rise by the same amount. The benefit is that this approach gives more focus on testing functionality rather than ensuring coverage. It does not ensure that the tests are completed, but perhaps it is an improvement.

It is interesting to note that NCover version 3 offers CC metrics. Only problem is that NCover can be set to fail the build if a metric falls below a threshold but it does not, of its own accord, detect falls from last value. Besides, if code is deleted, it is acceptable for the number of test cases to fall by the same difference as the CC.

Here the metric that matters, and should fall with time, is the difference between the test case count and the CC.

No comments: