Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run all modified JUnit test classes?

I have an IntelliJ project, versioned in git.

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

like image 307
slartidan Avatar asked May 23 '17 13:05

slartidan


People also ask

How do you run all test classes at once?

Run All Tests From Developer ConsoleGo to Setup | Developer Console. From the developer console Click Test | Run All. All the tests will run and a breakdown of the code coverage in the bottom right of the screen with the overall Code coverage and per-class code coverage is shown.


3 Answers

There is Changed Files predefined scope in Project tool window you can use to view all vcs changed files. From there select all JUnit classes and from the context menu create JUnit Run/Debug Configuration which will fill the classes pattern to run automatically.

like image 91
Andrey Avatar answered Oct 18 '22 19:10

Andrey


I do not believe there is any 'out of the box' solution to this that you can use. However you could script this in a language of your choice, to find the 'files changed in last commit' you can execute

git diff --name-only HEAD~

HEAD~ is a reference to the 'penultimate' commit, specifying only one commit reference to the git diff command will automatically compare to HEAD which is the latest commit.

You could take the output of this and iterate over it, perhaps if your test classes follow a similar naming scheme to your classes to test execute the tests by specifying a pattern for each file?

like image 1
Peter Reid Avatar answered Oct 18 '22 19:10

Peter Reid


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

like image 1
MattVonCoder Avatar answered Oct 18 '22 17:10

MattVonCoder