How to run all modified JUnit test classes?

Viewed 1887

I have an IntelliJ project, versioned in git.

How can I run all JUnit test classes, that I have modified since my last commit?

4 Answers

If your stack is composed of GIT and maven We came up with this shell script that is appended at the end of ours Jenkins Compilation jobs :

for s in `git diff --name-only HEAD~`
do
if [[ $s == *".java" ]]; then
  s=$(echo $s | tr '/' '.')
  s=$(echo ${s%.java})
  s=${s/.java/}
  s=${s/src./}Test
  echo "will test : $s"
  mvn -Dtest=$s  -DfailIfNoTests=false -Dfile.encoding=UTF-8 test-compile surefire:test
fi
done

Explanations :

Get the last commited files
for each
 if it is a java file
   convert name (in unix format) to FQN Test class name without extension
   run the maven test target

note : ours filenames for test follow the convention of appending Test at the end of the class file name. Adapt to your own conventions

This library seems to provide similar functionality: https://github.com/rpau/junit4git

This is a JUnit extension that ignores those tests that are not related with your last changes in your Git repository.

Related