Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve test name on TestNG

Tags:

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.

like image 988
Stan Kurilin Avatar asked Dec 21 '11 21:12

Stan Kurilin


People also ask

How can I check my test name in TestNG?

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.

What is test name in TestNG xml?

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.

How do I find test group name in TestNG?

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.

How do you get the test suite name?

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.


2 Answers

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)

like image 194
Dmitry Avatar answered Oct 25 '22 15:10

Dmitry


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" } 
like image 24
Tomasz Przybylski Avatar answered Oct 25 '22 16:10

Tomasz Przybylski