2

I have a Java project with a lot of classes. Now I want to get a list with all jUnit tests. My idea was to use grep for that.

So I navigated to the root folder, and use the following command:

grep junit -R ./ > output.txt

But obviously this isn't correct. So my question is. How is the correct command? And are there ways that are more easier to find jUnit tests?

4 Answers 4

4
find . -name *.java | egrep -R @Test . | cut -f1 -d" " | cut -f1 -d: > _1

If you've got junit 3 ones too, you could then run:

find . -name *Test.java >> _1
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Can you explain the first line a little bit?
@Roflcoptr It's grepping for @Test annotations and pulling the filename out. Doesn't work on OSX, though, get repeated filenames w/ no contextual info. One reason I like ack :)
2

You'd want:

grep -r junit *

But you need to be a little careful with that, as it'll go through all files, include jars.

Better to use find or ack:

ack --java -cl '@Test' # Print filenames with, and # occurences of, @Test annotations

Or more boringly:

find . -name *.java | xargs grep junit
ack --java junit # Or, more accurately:
ack --java 'import\s.*?junit.*'

Comments

2
find . -name "*.java" | xargs grep -il "junit"

In english: Starting at the current directory, find all files that end in ".java".
For each file found, do a case-insensitive search for "junit".
If "junit" is found, print only the file name.

Comments

0

I would grep for "extends TestCase" - results likely are going to be more reliable

Any class that extends TestCase is automatically a JUnit class.

Depending on whether you use jUnit 3 or 4 you may not have @Test annotations

3 Comments

Yes, but JUnit4 tests don't extend TestCase
@MatthewFarwell, really? can you post a reference confirming this? I was under impression that all jUnit tests (by definition) extend TestCase
TestCase is part of JUnit3, it still works with JUnit4, but isn't necessary. Try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.