简体   繁体   中英

How to run a single junit test from an ant project created by Netbeans from the command line?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM