Together: PIT, SonarQube and Gradle

This will be another “putting it all together” post, and you can jump to Putting Them Together below, but as a brief recap…

PIT

One of the testing tools I like to use is Pitest (PIT). It’s a mutation testing framework for Java.  Mutation testing introduced typical bugs into your code as mutations and then runs your tests to see if those errors are caught.  It’s testing the robustness of your tests.

SonarQube

SonarQube is a code quality testing tool. It offers a great why to perform a bunch of static analysis on your code, offering insights into test coverage, complexity, best practices etc. It runs as a free standing server and is often used by groups and organizations.  But if you’re comfortable with docker and docker hub it can be installed and running on a laptop in under two minutes, so there’s no reason not to use it as an individual too.

Gradle

Gradle is my build automation tool of choice…

Putting Them Together

Scattered about there’s information how to get these tools working together but I’ll try to cover it end to end here to make life simpler.

Gradle Driving Mutation Tests

This is simple to do, and the plugin documentation is good. For our purposes though there’s a bit of key configuration in your build.gradle:

plugins {
  id 'info.solidsoft.pitest' version '1.1.4'
}

pitest {
   threads = 4
   timestampedReports = false
   outputFormats = ['XML', 'HTML']
}

The key bit here is the timestampedReports = false.  By default PIT puts reports in a new directory every run.  We want the reports to consistently go to the same dirctory for automation’s sake. This flag does the trick. Once you have the above in place you can test it with:

    $ ./gradlew pitest

And you should find your report in ./build/reports/pitest/index.html.

SonarQube PIT Support

Next we need SonarQube to support PIT, this is done with a plugin and some settings. I’m assuming different versions of SonarQube will have variations on this so I’m not going to screen shoot every step but here’s how I did it with 6.1:

  1. Logged in as admin
  2. Selected “Administration” from the top bar menu
  3. Selected “System->Update Center” from the administration menu
  4. Selected “Available”
  5. Searched for “pitest”
  6. Selected “Install”

Okay, after the plugin is installed you’ll need to configure it:

  1. Back to the “Administration” page
  2. On the list there should now be “Pitest” select it
  3. Under “PIT activation mode” enter “reuseReport”
  4. Under “Output Directory for the PIT reports” enter “build/reports/pitest”

That should do it. The plugin is in place and knows how to play nice with gradle.

Gradle to SonarQube

Now gradle can generate the mutation tests, and SonarQube can accept them.  This is how you connect them up. First update your build.gradle with the SonarQube plugin:

plugins {
   id 'org.sonarqube' version '2.2.1'
}

And then you need to a tell the plugin where to find the SonarQube so add the appropriate URL to your gradle.properties:

systemProp.sonar.host.url=http://localhost:9000

With those two additions you should now be able to:

    $ ./gradlew pitest sonarqube

And it should all just work.

What Did We Get?

Now on SonarQube, for the project in question, there should be a new Mutation Analysis section on the Measures page with the results of PIT.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s