本文介绍了如何在@AfterMethod中从TestNG/Selenium获取测试结果状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于正在进行的研究,我需要从@AfterMethod运行测试方法(@Test)后捕获结果状态(通过/失败").

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.

我一直在使用导入org.testng.ITestResult;我的研究成果使访问了多个在线博客后使我的工作变得更加轻松,但是似乎结果并没有像预期的那样成功,因为即使断言失败

I have been using the import org.testng.ITestResult; as an out come of my research to get my work easier after going the several online blogs, but It seems like it didn't success my expectation as always the result outputs as passed, even though an assertion failed.

我的代码如下:

public class SampleTestForTestProject {
ITestResult result;

@Test(priority = 1)
public void testcase(){

    // intentionally failing the assertion to make the test method fail 
    boolean actual = true;
    boolean expected = false;
    Assert.assertEquals(actual, expected);

}

@AfterMethod
public void afterMethod()  {

    result = Reporter.getCurrentTestResult();

    switch (result.getStatus()) {
    case ITestResult.SUCCESS:
        System.out.println("======PASS=====");
        // my expected functionality here when passed
        break;

    case ITestResult.FAILURE:
        System.out.println("======FAIL=====");
        // my expected functionality here when passed
        break;

    case ITestResult.SKIP:
        System.out.println("======SKIP BLOCKED=====");
        // my expected functionality here when passed
        break;

    default:
        throw new RuntimeException("Invalid status");
    }
  }
}

控制台结果:

[TestNG] Running:  C:\Users\USER\AppData\Local\Temp\testng-eclipse--988445809\testng-customsuite.xml

======PASS=====

FAILED: testcaseFail
java.lang.AssertionError: expected [false] but found [true]

我的期望是将测试结果获取到一个变量以通过开关,如上面的代码片段所示,并在测试方法失败时显示"====== FAIL ====="

My expectation is to get the test result to a variable to get through the switch, as given in the above code snippet, and get printed "======FAIL=====" when the test method fail.

有人可以帮助我捕获每种测试方法(@Test)的执行测试结果.如果我采用的方法不正确,请帮我提供一个代码片段,以正确的方法.

Will someone be able assist me kindly to catch the execution test result for each test method (@Test). If the method I have approached is wrong, please assist me with a code snippet to the correct approach, kindly.

提前谢谢

推荐答案

做到这一点:

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();
   }

}

}

这篇关于如何在@AfterMethod中从TestNG/Selenium获取测试结果状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:08