How to print current executing JUnit test method while running all tests in a class or suite using ANT?

前端 未结 6 1324
野性不改
野性不改 2021-01-20 10:24

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 cla

6条回答
  •  一向
    一向 (楼主)
    2021-01-20 11:04

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

    Use a @Rule instead:

    import org.junit.rules.TestWatcher;
    import org.junit.runner.Description;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * Log the currently running test.
     * 
     * 

    Typical usage: * *

    {@code @Rule public LogTestName logTestName = new LogTestName();} * *

    See also: *
    {@link org.junit.Rule} *
    {@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() ); } }

    Notes:

    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 :-)

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

提交回复
热议问题