A long time ago, I met a development team which was working under big pressure by the quality team. Personally, I don’t like this kind of differences between development and quality teams, because it leads to development teams not feeling responsible for quality and to a confrontational relationship. They should be working collaboratively towards a unified goal of delivering a quality product.
One of the requirements was to have more than 85% of code coverage to ensure code quality. The result was perverse: development team wrote tests without assertions; they only invoked methods with different arguments to reach the desired percentage. It’s clear that they didn’t follow TDD.
Code coverage only gives us information about the percentage of code lines which are executed during tests.
Let’s see a way to verify that our current tests provide us with a safety net when we make changes to production code.
If we change the production code - replacing < with >=, swapping + with - or we return a different value in a given method - test battery should detect that change. In order words, tests should fail.
There are tools to make changes in production code automatically and to run tests in order to check if those changes are detected. It is usually referred to as follow:
Mutators: changes to be applied
Mutations: new versions of production code after applying mutators
Mutations are killed: tests fail; mutators are detected
So, we should aim at killing every mutation with tests.
When I heard about it I thought about that game I played when I was just a teenager: Super Pang.
And I imagined the following situation: balls are mutations of our production code and the child tries to break those balls with tests rays. Tests must be good enough to detect the balls and to break them.
It’s called mutation testing and it's a good way to make sure that you have a good safety net with your current tests to refactor production code or to add new features. It is as if you test your tests in order to get more information about their suitability.
Let’s see some examples with PIT and a simple Java project with problematic code.
A boundary value could be forgotten when writing tests (even following TDD). For example, this piece of production code:
status arg3 = ((from.getParam1() < from.getParam2())? BLACK: WHITE);
If we don’t have a test which considers the same value for param1 and param2, a mutation will survive when applying Conditional Boundary Mutator:
I try to avoid having logic in production code which is only used from test code.
It’s common to find equals and hashCode methods in Java which are only used in verifications or assertions. It’s easy to generate the code of these methods automatically with an IDE such as IntelliJ IDEA, but at the same time, it’s easy to have outdated code if we don’t remember to regenerate them when changing the involved class (or we don’t receive an alert about this fact).
For example, a property is added to a class without updating equals and hashCode methods, so PIT statistics results in:
Generated15 mutations Killed 6 (40%)Ran24 tests (1.6 tests per mutation)
And PIT report alerts on equals and hashCode methods.