Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the name of the test method that was run in a testng tear down method?

Basically, I have a teardown method that I want to log to the console which test was just run. How would I go about getting that string?

I can get the class name, but I want the actual method that was just executed.

public class TestSomething {      @AfterMethod     public void tearDown() {         System.out.println("The test that just ran was: " + getTestThatJustRanMethodName());     }      @Test     public void testCase() {        assertTrue(1 == 1);     } } 

...should output to the screen: "The test that just ran was: testCase"

However, I don't know the magic that getTestThatJustRanMethodName should actually be.

like image 264
Zee Spencer Avatar asked Jun 01 '10 18:06

Zee Spencer


People also ask

How do I find test group name in TestNG?

Use the reflected method to get the group of the test as below : @BeforeMethod public void befMet(Method m){ Test t = m. getAnnotation(Test. class); System.

How do you change the test name when using TestNG DataProvider?

Pass a ITestContext object to your @BeforeMethod and use setAttribute to set modified name in to the context object. You should make an if-else condition incase the name is not changed because some tests don't use DataProvider so the name is not changed.

Which marks a test method in TestNG?

@DataProvider Marks a method as supplying data for a test method.

What is @test method in TestNG?

@BeforeTest: This will be executed before the first @Test annotated method. It can be executed multiple times before the test case. @AfterTest: A method with this annotation will be executed when all @Test annotated methods complete the execution of those classes inside the <test> tag in the TestNG. xml file.


1 Answers

Declare a parameter of type ITestResult in your @AfterMethod and TestNG will inject it:

@AfterMethod public void afterMethod(ITestResult result) {   System.out.println("method name:" + result.getMethod().getMethodName()); } 
like image 124
Cedric Beust Avatar answered Oct 06 '22 08:10

Cedric Beust