使用命令行从 JUnit 类运行单个测试 [英] Run single test from a JUnit class using command-line

查看:45
本文介绍了使用命令行从 JUnit 类运行单个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试寻找一种方法,允许我仅使用命令行和 java 从 JUnit 类运行单个测试.

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

或:

java -cp .... org.junit.runner.JUnitCore org.package.classname#method

我注意到可能有一些方法可以使用 JUnit 注释来做到这一点,但我不想手动修改我的测试类的源代码(试图自动化).我也看到 Maven 可能有办法做到这一点,但如果可能的话,我想避免依赖 Maven.

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?

我正在寻找的关键点:

  • 能够从 JUnit 测试类运行单个测试
  • 命令行(使用 JUnit)
  • 避免修改测试源
  • 避免使用额外的工具

推荐答案

您可以相当轻松地制作自定义的准系统 JUnit 运行程序.下面是一个以 com.package.TestClass#methodName 形式运行单个测试方法的方法:

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);
    }
}

你可以这样调用它:

> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner 
    com.mycompany.product.MyTest#testB

快速查看 JUnit 源代码后,我得出与您相同的结论,即 JUnit 本身不支持此功能.这对我来说从来都不是问题,因为 IDE 都有自定义的 JUnit 集成,允许您在光标下运行测试方法以及其他操作.我从未直接从命令行运行 JUnit 测试;我总是让 IDE 或构建工具(Ant、Maven)来处理它.特别是因为默认的 CLI 入口点 (JUnitCore) 除了测试失败时的非零退出代码外不会产生任何结果输出.

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 版本 >= 4.9,您需要 hamcrest 类路径中的库

NOTE: for JUnit version >= 4.9 you need hamcrest library in classpath

这篇关于使用命令行从 JUnit 类运行单个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆