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

Wir verwenden Cookies, um unsere Webseite für Sie zu optimieren. Mit dem Besuch unserer Webseite erklären Sie sich damit einverstanden. // Our website is using cookies to improve your experience. By continuing to browse the site, you are agreeing to our use of cookies.

Weitere Informationen finden Sie in unserer Datenschutzerklärung. // For more information, please refer to our privacy policy.

contact icon

Contact us now