简体   繁体   English

TestNG 侦听器的 TestNG BeforeClass 和 AfterMethod 等效方法

[英]TestNG BeforeClass and AfterMethod equivalent methods for TestNG listener

Taking the below base class methods testSetup() and getStatusAndAnnotation() and putting them into TestNG Listener.采用以下base class methods testSetup() and getStatusAndAnnotation()并将它们放入 TestNG Listener 中。 I could find the equivalent of @BeforeClass as onBeforeClass(ITestClass, IMethodInstance) .我可以找到与@BeforeClass等效的onBeforeClass(ITestClass, IMethodInstance) However, what is the equivalent for @AfterMethod ?但是, @AfterMethod的等价物是@AfterMethod

My aim is to pass the ITestResult as a parameter to that method because I will be holding the test result into it.我的目标是将 ITestResult 作为参数传递给该方法,因为我会将测试结果保存在其中。 The code below shows that I am getting the annotation and status of test and then pushing it to the testrail cloud.下面的代码显示我正在获取测试的注释和状态,然后将其推送到 testrail 云。 I tried using:我尝试使用:

@Override
    public void afterInvocation(IInvokedMethod, ITestResult) {
        ..
    }

It did not help and gave它没有帮助,并给了

java.lang.NoSuchMethodException: client.test.reporting.listeners.TestRailListener.test_getVersion()
    at java.lang.Class.getMethod(Class.java:1670)

Here Java: 1670 is这里 Java:1670 是

 throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));

**@BeforeClass
        public static void testSetup() throws InterruptedException, IOException, APIException {
            initTestRail();
        }
@AfterMethod
public void getStatusAndAnnotation(ITestResult result) throws NoSuchMethodException, IOException, APIException {
    HashMap<Object, Object> map = new HashMap<>();
    Method m = getClass().getMethod(result.getName());
    TestCase annotation = m.getAnnotation(TestCase.class);
    try {
        map.put("testRailCaseId",annotation.testRailCaseId());
    }catch (NullPointerException e){}
    if (result.isSuccess()) {
        map.put("result", 1);
    } else {
        map.put("result", 5);
    }
    if(annotation.testRailDeployEnable() && !map.get("testRailCaseId").equals(null) && !map.get("testRailCaseId").toString().isEmpty())
    {
        TestRailIntegration.addTestResult(map);
    }
    else System.out.println("Deploying result was canceled, because test has annotation \"testRailDeployEnable: false\" or \"testRailCaseId\" has no value");
}**

After every method is executed, whatever maybe the status (PASS/FAIL/SKIP), I want to execute this getStatusAndAnnotation...() but it gives above exception/error.执行每个方法后,无论状态如何(通过/失败/跳过),我都想执行此 getStatusAndAnnotation...() 但它给出了上述异常/错误。

我认为你可以实现org.testng.ITestListener ,但不是单个方法afterInvocation()它有几个取决于结果: onTestSuccess()onTestFailure()onTestSkipped()等。每个方法都传递ITestResult对象,这也包含正在执行的方法:

@Override public void onTestSuccess(ITestResult iTestResult) { Method method = iTestResult.getMethod().getConstructorOrMethod().getMethod(); Annotation[] annotations = method.getDeclaredAnnotations(); ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM