Faking Line Coverage
Line Coverage is a metric that measures how many and which lines have been covered during execution. There are various tools to measure coverage.- Jacoco – Measuring on ByteCode level which has the advantage that you can test your actual artifacts, but bytecode can be different to its source at times.
- ECobertura, Clover – Measuring on SourceCode level which is more precise than byte-code measuring but injects additional code before compilation, ending up in different artifacts than you want to deliver.
@Test
public void test() {
subject.invokeSomeMethod();
}
Obviously this test is broken because in cannot fail – unless the code itself produces an exception. But with tests like these you may achieve quite easily a high line coverage and a stable test suite.
But typical programs are rarely linear and have some sort of loop or branch constructs. So it’s unlikely you achieve 100% line coverage. So we have to fake branch coverage, too.
Faking Condition Coverage
Lets assume our simple program consists of the following code
Object compute(Object input) {
if("left".equals(input) {
return "right";
}
return "left";
}
It has one condition with two branches. With a single test, you may get 66% Line Coverage and 50% Condition Coverage. I’ve experiences several times that branch coverage is perceived as “better” or of “more value” because it’s harder to achieve. If “harder” means “more code” it’s certainly true, but branch coverage suffers the same basic problem as line coverage does: it’s just a measure for which code is executed and not how good your tests are. It also depends on the code base, what is harder to achieve. If the happy-flow you test covers only a minor part of the code, you may have 50% branch-coverage but only 10% line coverage. Given the above example, assume the “left”-branch contains 10 lines of code, but you only test for the “right”-branch.
But as we are developers who want to make happy managers, let’s fake branch coverage!
Given, we only test a single flow in a single test, we need two tests:
@Test
public void testLeft() {
String output = compute("left");
}
@Test
public void testRight() {
String output = compute("right");
}
This test will produce 100% branch- and line coverage and is very unlikely to fail, ever.
But again: it’s worthless, because we don’t check any output of the operation. So the operation may return anything without failing the test. But still in terms of KPI metrics we achieved:
- 2 tests for 1 method (great ratio!)
- 100% line coverage
- 100% condition coverage
So in the next post, I'll show you how to fake assertions.
No comments:
Post a Comment