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.
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.
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.
@DataProvider Marks a method as supplying data for a test method.
@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.
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()); }
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