Reputation: 1953
This answer works if you don't mind having extra collateral to remember/set the CLASSPATH
: How to run JUnit test cases from the command line. But, if the project was created by Netbeans, the classpath is already in your project, and the project 'knows' how to run single unit tests and single unit test methods.
$ ant -p
. . .
test-single Run single unit test.
test-single-method Run single unit test.
. . .
However, if you try to build these targets, you will be presented with this error message (and then others) with no guidance on how to set the properties in question:
BUILD FAILED
/home/me/myproject/nbproject/build-impl.xml:1300: Must select some files in the IDE or set javac.includes
Set the properties incorrectly, and ant will simply tell you that the build succeeded, but not compile nor run your test. This is harder than it should be.
So, how do we set the properties that these targets require?
Upvotes: 3
Views: 11855
Reputation: 1953
With some experimentation, I discovered these things.
To build the test-single
target:
Set both javac.includes
and test.includes
to the path to the .java
source file of the junit test in question. The path must be relative
to the project's test
folder. Example:
$ ant test-single -Djavac.includes=my/company/MyTest.java -Dtest.includes=my/company/MyTest.java
To build the test-single-method
target:
In addition, set test.class
to fully qualified name of your test class
and set test.method
to the name of the test method. Example:
$ ant test-single-method -Djavac.includes=my/company/MyTest.java -Dtest.includes=my/company/MyTest.java -Dtest.class=my.company.MyTest -Dtest.method=testSomething
Yes, the parameters are highly repetitive and begging to be scripted.
I wrapped test-single
in a script that requires only the path to the
.java source.
I would like to wrap test-single-method
in a script that accepts the
path to the .java source file and the method, but I haven't learned
how to do the string magic yet.
Upvotes: 6