Skip to content
Advertisement

Retrieve test name on TestNG

Can I retrieve currently running test name like in JUnit (using getName() or rules)?

@Test
public void fooBar(){
     System.out.println(magic()); //should print "fooBar"
}

P.S. I don’t want use some self-written tool based on stack traces.

Advertisement

Answer

According the to TestNG documentation at: http://testng.org/doc/documentation-main.html you can implement listeners that might be able to help you with your problem.

Look at section 5.16 TestNG Listeners, and in particular the IInvokedMethodListener (javadoc: http://testng.org/javadocs/org/testng/IInvokedMethodListener.html). You can hook into the beforeInvocation to grab the method name, hold onto it somewhere, and then use it in your test. You could of course, just use the the details immediately in your listener implementation.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement