简体   繁体   English

使用命令行从 JUnit 类运行单个测试

[英]Run single test from a JUnit class using command-line

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.我试图找到一种方法,允许我仅使用命令行和 java 从 JUnit 类运行单个测试。

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).我注意到可能有办法使用 JUnit 注释来做到这一点,但我不希望手动修改我的测试类的源(试图自动化)。 I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.我也确实看到 Maven 可能有办法做到这一点,但如果可能的话,我想避免依赖 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能够从 JUnit 测试类运行单个测试
  • Command Line (using JUnit)命令行(使用 JUnit)
  • Avoid modifying the test source避免修改测试源
  • Avoid using additional tools避免使用额外的工具

You can make a custom, barebones JUnit runner fairly easily.您可以相当轻松地制作自定义的准系统 JUnit 运行程序。 Here's one that will run a single test method in the form com.package.TestClass#methodName :这是一个将以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.在快速查看 JUnit 源代码后,我得出与您相同的结论,即 JUnit 本身不支持此功能。 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.这对我来说从来都不是问题,因为 IDE 都有自定义的 JUnit 集成,允许您在光标下运行测试方法以及其他操作。 I have never run JUnit tests from the command line directly;我从未直接从命令行运行 JUnit 测试; I have always let either the IDE or build tool (Ant, Maven) take care of it.我总是让 IDE 或构建工具(Ant、Maven)来处理它。 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).特别是因为默认的 CLI 入口点 (JUnitCore) 除了测试失败时的非零退出代码之外不会产生任何结果输出。

NOTE: for JUnit version >= 4.9 you need hamcrest library in classpath注意:对于 JUnit 版本 >= 4.9,您需要在类路径中使用hamcrest

I use Maven to build my project, and use SureFire maven plugin to run junit tests.我使用 Maven 构建我的项目,并使用 SureFire maven 插件运行 junit 测试。 Provided you have this setup, then you could do:如果您有此设置,那么您可以执行以下操作:

mvn -Dtest=GreatTestClass#testMethod test

In this example, we just run a test method named "testMethod" within Class "GreatTestClass".在这个例子中,我们只是在类“GreatTestClass”中运行一个名为“testMethod”的测试方法。

For more details, check out http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html有关更多详细信息,请查看http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

以下命令工作正常。

mvn -Dtest=SqsConsumerTest -DfailIfNoTests=false test

We used IntelliJ, and spent quite a bit of time trying to figure it out too.我们使用了 IntelliJ,也花了很多时间试图弄明白。

Basically, it involves 2 steps:基本上,它包括两个步骤:

Step 1: Compile the Test Class步骤 1:编译测试类

% javac -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" SetTest.java

Step 2: Run the Test步骤 2:运行测试

% java -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" org.junit.runner.JUnitCore SetTest

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

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