- Number of Unit Tests (Total, Failed, Successful)
- Line Coverage
- Branch Coverage
This post is about to show how to game the system and life-hack those KPIs to fake good quality. It’s NOT a best practice but a heads up to those who make decision based on those metrics to look behind the values.
Faking Number of Unit Tests
Most (if not all?) frameworks count the number of tests executed, which failed and which succeeded. A high number of tests is usually perceived as a good indicator of quality. The increase of the amount of tests should correlate with the increase in lines of code (another false-friend KPI). But what is counted as a test?Let’s look at the Junit which is the de-facto standard for developing and executing Java based unit tests, but other frameworks such as TestNG follow similar concepts.
In Junit 3 it was every parameterless public void method starting with “test” in a class extending TestCase. Since Junit 4 every method annotated with @Test counts as a Test.
That’s it. Just a name convention or an Annotation and you have your test, so let’s fake it!
@Test public void test() { }
This is pure gold: a stable and ever succeeding Unit Test!
Copy and paste or even generate those and you produce a test suite satisfying the criteria:
- Big, tons of tests, probable even more than you have LoCs
- Stable, none of these tests is failing. Ever.
- Fast, you have feedback about the success within seconds.
- It doesn’t run any code
- It doesn’t pose any assertion about the outcome
In the upcoming posts we'll have a look into both.