PITest Setup with Maven

Written by: Tom Spencer

Jun 28, 20222 min read

PITest

PIT provides parallel isolated tests and is useful for Mutation testing on applications to ensure test coverage. pitest Mutation testing is a type of Software Testing that is performed to evaluate the quality of an application's test suite. Mutation testing involves modifying the program in small ways. The coverage report provided by PITest can help to make tests more effective or locate weaknesses in the test data used for the program. In this article we will look at how to set up PIT with an existing project.

Maven setup

In order to setup the PITest library, we need to include the pitest-maven plugin in our pom.xml for our project:

<plugins>
  <plugin>
    <groupId>org.pitest</groupId>
    <artifactId>pitest-maven</artifactId>
    <version>1.7.3</version>
    <dependencies>
      <dependency>
        <groupId>org.pitest</groupId>
        <artifactId>pitest-junit5-plugin</artifactId>
        <version>0.15</version>
      </dependency>
    </dependencies>
  </plugin>
</plugins>

We can then add specific configuration for our project:

<plugins>
  <plugin>
    <configuration>
      <failWhenNoMutations>false</failWhenNoMutations>
      <timestampedReports>false</timestampedReports>
      <targetClasses>
        <param>com.example.*</param>
      </targetClasses>
      <targetTests>
        <param>com.example.*</param>
      </targetTests>
      <excludedTestClasses>
        <param>
          com.example.quiz.HexagonalArchitectureTest
        </param>
      </excludedTestClasses>
      <excludedClasses>
        <param>com.example.quiz.domain.ResponseId</param>
      </excludedClasses>
      <excludedMethods>
        <param>equals</param>
        <param>hashCode</param>
        <param>toString</param>
      </excludedMethods>
      <reportsDirectory>${project.basedir}/docs/coverage</reportsDirectory>
    </configuration>
  </plugin>
</plugins>

Here we have specified the target classes for our project and the tests. We use a glob to specify that we want all files in those packages included for PITest. I have added flags for failWhenNoMutations and timeStampedReports and set them to false.

In my project, I have ArchUnit (to be described in a later post) and have opted to exclude these tests with the configuration excludedTestClasses. I have also opted to exclude classes that don't need to be run and excludedMethods for equals, hashCode and toString as these functions are Java inbuilt and do not need to be tested.

For reports I have specified the root directory /docs/coverage which is where I store the reports after running the PITest command. The maven command to run PITest is:

mvn org.pitest:pitest-maven:mutationCoverage

But it is also possible to run this using the maven goal from IntelliJ like so: Maven goal

Test coverage

Once you have run PITest you are then provided with a test report. I include a viewable test report for my Quiz application here quiz coverage

Extra

Try setting up and running PITest on your own project and then viewing the reports to check coverage.