使用命令行从JUnit类运行单个测试 [英] Run single test from a JUnit class using command-line
问题描述
我试图找到一个方法,将允许我从一个JUnit类只使用命令行和java运行单个测试。
我可以运行从类中使用以下的整套测试:
java -cp .... org.junit.runner.JUnitCore org .package.classname
我真正想做的是这样的:
java -cp .... org.junit.runner.JUnitCore org.package.classname.method
或:
java -cp ... 。org.junit.runner.JUnitCore org.package.classname#method
我注意到使用JUnit注释来做到这一点的方法,但我不想手动修改我的测试类的源(尝试自动化)。我也看到Maven可能有办法做到这一点,但如果可能,我想避免依赖Maven。
所以我想知道是否有任何方式
我要找的关键点:
- 能够从JUnit测试类运行单个测试
- 命令行(使用JUnit)
- 避免修改测试来源
- 避免使用其他工具
你可以很容易地做一个自定义的,准系统JUnit转轮。下面是一个将以 com.package.TestClass#methodName
形式运行单个测试方法的方法:
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
public class SingleJUnitTestRunner {
public static void main(String ... args)throws ClassNotFoundException {
String [] classAndMethod = args [0] .split(#) ;
请求request = Request.method(Class.forName(classAndMethod [0]),
classAndMethod [1]);
结果result = new JUnitCore()。run(request);
System.exit(result.wasSuccessful()?0:1);
}
}
您可以这样调用:
> java -cp path / to / testclasses:path / to / junit-4.8.2.jar SingleJUnitTestRunner
com.mycompany.product.MyTest#testB
快速查看JUnit源代码后,我得出了与JUnit不支持这个结论相同的结论。这从来没有是我的问题,因为IDEs都有自定义JUnit集成,允许您运行测试方法下的游标,以及其他操作。我从来没有从命令行直接运行JUnit测试;我总是让IDE或构建工具(Ant,Maven)来处理它。特别是因为缺省CLI入口点(JUnitCore)在测试失败时不产生非零退出代码以外的任何结果输出。
I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java.
I can run the whole set of tests from the class using the following:
java -cp .... org.junit.runner.JUnitCore org.package.classname
What I really want to do is something like this:
java -cp .... org.junit.runner.JUnitCore org.package.classname.method
or:
java -cp .... org.junit.runner.JUnitCore org.package.classname#method
I noticed that there might be ways to do this using JUnit annotations, but I would prefer to not modify the source of my test classes by hand (attempting to automate this). I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.
So I am wondering if there is any way to do this?
Key points I'm looking for:
- Ability to run a single test from a JUnit test class
- Command Line (using JUnit)
- Avoid modifying the test source
- Avoid using additional tools
You can make a custom, barebones JUnit runner fairly easily. Here's one that will run a single test method in the form com.package.TestClass#methodName
:
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
public class SingleJUnitTestRunner {
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
You can invoke it like this:
> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner
com.mycompany.product.MyTest#testB
After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn't produce any result output other than a non-zero exit code on test failure(s).
这篇关于使用命令行从JUnit类运行单个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!