使用 ANT 在类或套件中运行所有测试时,如何打印当前正在执行的 JUnit 测试方法? [英] How to print current executing JUnit test method while running all tests in a class or suite using ANT?

查看:26
本文介绍了使用 ANT 在类或套件中运行所有测试时,如何打印当前正在执行的 JUnit 测试方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组 JUnit 测试用例,我使用 junit 从 ANT 执行它们 任务.在执行测试时,在控制台中我只能看到当前正在运行的测试用例(即 Java 类),而不是测试方法.有没有办法打印当前正在执行的测试方法?或者除了拥有自己的 JUnit 测试运行器之外,还有其他方法可以做到这一点吗?

I have a set of JUnit Test Cases and I execute them from ANT using the junit task. While executing the tests, in the console I get to see only which test case (i.e. Java class) is currently running, but not the test method. Is there a way I can print the current executing test method? Or is there any other way to do this other than having my own JUnit test runner?

示例控制台输出

[junit] Running examples.TestCase1
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 2.835 sec

相反,我希望得到这样的输出

Instead I wish to get the output like

[junit] Running examples.TestCase1
[junit] Running examples.TestCase1.test1
[junit] Running examples.TestCase1.test2
[junit] Running examples.TestCase1.test3
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 2.835 sec

推荐答案

不幸的是,没有很好的方法可以挂接到 JUnit.对于一个用 TDD 开发的框架来说,它的敌意惊人.

Unfortunately, there is no good way to hook into JUnit. For a framework that was developed with TDD, it's astonishingly hostile.

使用 @Rule 代替:

import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Log the currently running test.
 * 
 * <p>Typical usage:
 * 
 * <p>{@code @Rule public LogTestName logTestName = new LogTestName();}
 *
 * <p>See also:
 * <br>{@link org.junit.Rule}
 * <br>{@link org.junit.rules.TestWatcher}
 */
public class LogTestName extends TestWatcher {

    private final static Logger log = LoggerFactory.getLogger( "junit.logTestName" );

    @Override
    protected void starting( Description description ) {
        log.debug( "Test {}", description.getMethodName() );
    }

}

注意事项:

我正在使用静态记录器.这使得代码执行得更快,但我的主要原因是记录测试名称是一个交叉问题:我想在一个中心位置启用/禁用此日志记录,而不是为每个测试类配置它.

I'm using a static logger. That makes the code execute faster but my main reason is that logging test names is a cross-cutting concern: I would like to enable/disable this logging in a central place instead of configuring it for each test class.

另一个原因是我有处理构建输出的工具,这些工具更容易配置为固定模式:-)

Another reason is that I have tools which process the build output and those are easier to configure for a fixed pattern :-)

如果您不想要这个,那么只需获取 description.getTestClass() 的记录器.

If you don't want this, then just get the logger for description.getTestClass().

这篇关于使用 ANT 在类或套件中运行所有测试时,如何打印当前正在执行的 JUnit 测试方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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