有没有办法在 After 方法中使用 ITestResult 获取完整的错误/异常?

2023-12-28

@Test(priority = 16, enabled = true)
    public void C2410859_FilterAlertsByCard()
            throws IOException, InterruptedException, ATUTestRecorderException, APIException {

        record.start();
        launchUrl();
        startResult();
        startTestCase(Thread.currentThread().getStackTrace()[1].getMethodName().toString());
        LoginApplication(username, password);
        HomePage homePage = new HomePage();
        homePage.MyAlerts.click();
        MyAlerts myalerts = new MyAlerts();

    }




    @AfterMethod
    public void afterMethod(ITestResult result) throws ATUTestRecorderException {
        resultOfTest(result);   
        endTestcase();
        endResult();
        record.stop();
        closeBrowsers();
    }

测试结果:

public static void resultOfTest(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 ***********");
                test.fail("This test is failed due to "+ result.getThrowable());

            }

             else if(result.getStatus() == ITestResult.SKIP ){

                System.out.println("Skiped***********");

            }
        }
           catch(Exception e)
           {
             e.printStackTrace();
           }
    }

这仅显示一个线性错误(例如,空指针异常)。有没有办法在 After 方法中使用 ITestResult 获取完整的错误/异常?


如果 try catch() 块没有处理,您可以在 AfterMethod 中通过 ITestResult 对象获取它。

例子:

异常类型:java.lang.ArithmeticException:/按零

@Test
public void testDemo() {
    int i;
    i = 9 / 0;
}

@AfterMethod
public void afterMethod(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
            String testResult = result.getThrowable();
            System.out.println(testResult);
    } 
}

这里testDemo方法由于算术异常而失败,因此ITestResult有fail方法。通过使用result.getThrowable();方法你可以有异常描述。

但是,如果事情由 try-catch() 块处理,则 AtferMethod 中不会有异常详细信息。

@Test
public void testDemo() {
    try {
        int i;
        i = 9 / 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@AfterMethod
public void afterMethod(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
            String testResult = result.getThrowable();
            System.out.println(testResult);
    } 
}

这里的事情是由 try-catch 块处理的,所以它不会失败,测试方法会通过。因此,您不能将 ITestResult 中的异常详细信息视为失败。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法在 After 方法中使用 ITestResult 获取完整的错误/异常? 的相关文章

  • 关闭 swi-prolog 中的警告

    如何关闭 swi prolog 中的警告 Clauses of XXX AA are not together in the source file 很烦人 相反 您可以修复该警告 The 不连续指令 http www swi prolog

随机推荐