Skip to content

Retrieve test name on TestNG

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

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.



According the to TestNG documentation at: 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: 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.

1 People found this is helpful