Mutation testing with Pitest

Mutation testing with Pitest

Problem

Common test metrics, such as line coverage and branch coverage, only indicate whether code has been executed during testing and not necessarily if it is working correctly.

This makes it possible to write test code that merely satisfies the metrics without testing anything (test cases without assertions).

Solution

To get an idea of the quality of the tests, mutation testing can be used.

A mutation test performs unit tests on different mutations of the code to be tested.

There are many different types of mutations, e.g. changing conditions (>= becomes >) or inverting boolean return values.

Example

public class Jambitee {
  public boolean isAwake(int amountOfCoffeesToday) {
    if (amountOfCoffeesToday >= 1 ) {
      return true;
    } else {
      return false;
    }
  }
}

public class JambiteeTest {
  private Jambitee jambitee = new Jambitee();

  @Test
  public void isAwakeWith2Coffees() {
    assertTrue(jambitee.isAwake(2));
  }

  @Test
  public void isAsleepWithNoCoffee() {
    assertFalse(jambitee.isAwake(0));
  }
}
Although the code here has 100% line and branch coverage, testing does not secure the ">=1" condition.
In Maven, you only need to include the plugin.
<plugin>
  <groupId>org.pitest</groupId>
  <artifactId>pitest-maven</artifactId>
  <version>LATEST</version>
</plugin>
After that, you can perform the mutation testing by calling the mutationCoverage Goal.`mvn org.pitest:pitest-maven:mutationCoverage`
Mutationtests mit Pitest

Pitest runs the tests on different mutations and detects mutations that are not covered by the tests.

Further Aspects

---

Author: Christof Huber / Software Developer / Business Division Automotive Bavaria

Mutation testing with Pitest

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now