rpau / junit4git

Junit Extensions for Test Impact Analysis

Version Matrix


Build Status codecov

This is a JUnit extension that ignores those tests that are not related with your last changes in your Git repository. This is not a new idea, since big companies, specially with big mono-repos have worked on it.

Martin Fowler also describes how to use the test impact to infer the minimum tests to run.

You can use it from Maven and Gradle projects in any Junit based project (written in any JVM based language: e.g Kotlin, Java, Scala)

Getting Started

These instructions will get you a copy of the project up and running on your local machine.


  • JRE 8
  • Git
  • Maven or Gradle
  • Master as a default base branch

1. Configure your build

1.1 If you are using Maven

Declare a new test dependency in your pom.xml:


Add/Edit also the maven-surefire-plugin adding the listener property:


Commit these changes into your master branch.

git checkout master
git add pom.xml
git commit -m 'junit4git setup'

1.1 If you are using Gradle

You need to use Junit 5. In such case, modify your build.gradle file with the following contents:


And then:

git checkout master
git add pom.xml
git commit -m 'junit4git setup'

2. Generate a Test Impact Report

After having configured your build, run your master branch tests.

mvn test -- or gradle check
[WARNING] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

A Test Impact Report is generated as a Git note at (refs/notes/tests). You can check the contents with:

export GIT_NOTES_REF=refs/notes/tests
git notes show

A report, similar to the next one, will be printed in your console.

    "test": "CalculatorTest",
    "method": "testSum",
    "classes": [

This report specifies classes that are instantiated for each one of your test methods.

3. Run the Impacted Tests

After generating your test impact report, run the tests again (from the master branch or in a new local branch)

mvn tests -- or gradle check
[WARNING] Tests run: 0, Failures: 0, Errors: 0, Skipped: 4

VoilĂ ! You will see that all the tests have been skipped because there is no change that may affect in our test results.

However, if you add / modify new test files or modify an existing source file, only the affected tests are executed.

4. Use Travis to Upgrade the Test Impact Report

Tests can be run incrementally once the base testing report is generated. However, how can we make them updated everytime a pull request is merged?

Our strategy consist of delagating this work to Travis with the following steps:

  • Generate an OAuth token for your GitHub User. Go to Settings > Developer Settings > Personal access tokens. Then Generate new token with repo permissions. Copy the generated token into the clipboard.
  • Add a new Travis Environment Variable. Go to your last build, and then: More Options > Settings > Environment Variables. Add an environment variable named GITHUB_TOKEN and paste the contents of your clipboad.
  • Edit your .travis.yml file adding the following contents:
  - echo -e "machine github.com\n  login $GITHUB_TOKEN" >> ~/.netrc
  - git push origin refs/notes/tests:refs/notes/tests
  • Commit your .travis.yml changes to master.


This project is licensed under The Apache Software License.


This is a Gradle project that you can easily build running

gradle build

Or, if you want to generate a version to test it from a Maven project

gradle publishToMavenLocal