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.
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.
In the above source code of xml file, suite is at the higher hierarchy in TestNG. Inside the , you have to define the test name folder. This test name folder is the name of the folder.
The Complete TestNG & Automation Framework Design Course If the user wants to get the group name of a test method prior to its execution, then @BeforeMethod can be useful to retrieve it. On the other hand, if the user wants to know the group of test method is just executed, then @AfterMethod can be used.
Suppose the user wants to retrieve the name of a test suite before the execution. In this case, the code will be written inside @BeforeSuite to retrieve suite name that will be executed. As @BeforeSuite executes at first, the suite name will be printed before the execution of any test methods.
I found better solution with @BeforeMethod annotation:
import java.lang.reflect.Method; public class Test { @BeforeMethod public void handleTestMethodName(Method method) { String testName = method.getName(); ... } ... }
(based on solution from this thread)
When you use TestNG you can use @BeforeTest
annotation
Try set test name
in testng.xml file test tag:
<test name="Check name test" >
and use this metod:
@BeforeTest public void startTest(final ITestContext testContext) { System.out.println(testContext.getName()); // it prints "Check name test" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With