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

后端 未结 2 1096
一整个雨季
一整个雨季 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:19

    just do it:

    public class stacktest  {
    
    
    @Test
    public void teststackquestion() {
    
        boolean actual = true;
        boolean expected = false;
       Assert.assertEquals(actual, expected);
    
    }
    
    
    @AfterMethod
    public void afterMethod(ITestResult result)
    {
        try
     {
        if(result.getStatus() == ITestResult.SUCCESS)
        {
    
            //Do something here
            System.out.println("passed **********");
        }
    
        else if(result.getStatus() == ITestResult.FAILURE)
        {
             //Do something here
            System.out.println("Failed ***********");
    
        }
    
         else if(result.getStatus() == ITestResult.SKIP ){
    
            System.out.println("Skiped***********");
    
        }
    }
       catch(Exception e)
       {
         e.printStackTrace();
       }
    
    }
    

    }

    0 讨论(0)
  • 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
    
    }
    
    0 讨论(0)
提交回复
热议问题