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

眉间皱痕 提交于 2019-12-31 02:28:07

问题


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?

Sample Console Output

[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

回答1:


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.
 * 
 * <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() );
    }

}

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().




回答2:


You can try using the formatter by setting type as plain.

<formatter type="plain" usefile="false"/>

Or

You can use implement Custom formatters by extending org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter class.




回答3:


Derive a class from RunListener and override the testStarted() method. It receives a Description parameter from which you can obtain the test class and the test method -- e.g., description.getMethodName().




回答4:


If you do not want to create your own test runner you could try using the stacktrace.

The following Method prints the class and method name of the method that called it.

public static void printMethod() {
    System.out.println(Thread.currentThread().getStackTrace()[2]);
}

You would have to call this method in each of your test methods.




回答5:


Adding showoutput="true" caused my junit tests to log output as the tests were executing:

junit fork="yes" showoutput="true" printsummary="withOutAndErr"



回答6:


You can make use of the TestName rule. Just add the following code to your test class:

@Rule
public TestName testName = new TestName();

@Before
public void printTestMethod() {
    System.out.println("Running " + testName.getMethodName());
}


来源:https://stackoverflow.com/questions/15222376/how-to-print-current-executing-junit-test-method-while-running-all-tests-in-a-cl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!