简体   繁体   English

如何从NetBeans从命令行创建的ant项目中运行单个junit测试?

[英]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 . 如果您不介意额外记住/设置CLASSPATH则此答案有效: 如何从命令行运行JUnit测试用例 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. 但是,如果项目是由Netbeans创建的,则类路径已经在您的项目中,并且该项目“知道”如何运行单个单元测试和单个单元测试方法。

$ 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. 错误地设置属性,并且ant只会告诉您构建成功,但是不会编译或运行测试。 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: 要构建test-single目标:

Set both javac.includes and test.includes to the path to the .java source file of the junit test in question. javac.includestest.includes都设置为所涉及的junit测试的.java源文件的路径。 The path must be relative to the project's test folder. 该路径必须相对于项目的test文件夹。 Example: 例:

$ ant test-single -Djavac.includes=my/company/MyTest.java -Dtest.includes=my/company/MyTest.java

To build the test-single-method target: 要构建test-single-method目标:

In addition, set test.class to fully qualified name of your test class and set test.method to the name of the test method. 另外,将test.class设置为测试类的完全限定名称,并将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. 我将test-single包装在仅需要.java源代码路径的脚本中。

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. 我想将test-single-method封装在一个脚本中,该脚本接受指向.java源文件和方法的路径,但是我还没有学会如何做字符串魔术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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