How to get the test result status from TestNG/Selenium in @AfterMethod?

后端 未结 2 1100
一整个雨季
一整个雨季 2021-02-08 00:42

For a research I\'m doing, I\'m in need of capturing the result status (Passed/Failed) after running the test method (@Test), from @AfterMethod.

I have been using the im

2条回答
  •  旧巷少年郎
    2021-02-08 01:28

    The TestListenerAdapter has methods for each of those situations (success, skipped, failure). My suggestions is to make your own listener like this.

    public class MyTestResultListener extends TestListenerAdapter {
    
        @Override
        public void onTestFailure(ITestResult result) {
            // do what you want to do
        }
    
        @Override
        public void onTestSuccess(ITestResult result) {
            // do what you want to do
        }
    
       @Override
        public void onTestSkipped(ITestResult result) {
            // do what you want to do
        }
    }
    

    Then add your listener to the test class.

    @Listeners(MyTestResultListener.class)
    public class MyTest {
    
    // your tests
    
    }
    

提交回复
热议问题