How to print the current executable junit test method during all tests in a class or set using ANT?

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

Console Output Example

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

Instead, I want to get a result 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
+2
source share
5 answers

, JUnit. , TDD, .

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

}

:

. , , : / , .

, , , : -)

, description.getTestClass().

+7

formatter, .

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

, org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter.

+1

, stacktrace.

.

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

.

0

showoutput="true" junit :

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

Derive a class from RunListenerand override the method testStarted(). He gets the option Descriptionfrom which you can get a test class and test methods - such as description.getMethodName().

0
source

Source: https://habr.com/ru/post/1543960/


All Articles