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.
If you want to get the method name before the test is executed you can use the following:
Just declare a
java.lang.reflect.Method
parameter.But TestNG allows you to inject a lot more ;)
Declare a parameter of type ITestResult in your @AfterMethod and TestNG will inject it:
Another (although not as simple as Cedric's answer) way that TestNG supports this is to register a listener:
Where the listener could look like this:
This particular listener would print the method name (i.e.
someTest
) to the console. It would be executed after every executed test.If you are generating the testSuite programmatically then you can add the listener as follows instead of adding
@Listeners({MethodListener.class})
over each test class