【问题标题】:TestNG BeforeClass and AfterMethod equivalent methods for TestNG listenerTestNG 监听器的 TestNG BeforeClass 和 AfterMethod 等效方法
【发布时间】:2016-04-18 19:29:14
【问题描述】:

将下面的base class methods testSetup() and getStatusAndAnnotation() 放入TestNG Listener。我可以找到与@BeforeClass 等效的onBeforeClass(ITestClass, IMethodInstance)。但是,@AfterMethod 的等价物是什么?

我的目标是将 ITestResult 作为参数传递给该方法,因为我会将测试结果保存在其中。下面的代码显示我正在获取测试的注释和状态,然后将其推送到测试轨云。我尝试使用:

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

没有帮助,给了

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

这里的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");
}**

在执行每个方法后,无论状态如何(PASS/FAIL/SKIP),我都想执行这个 getStatusAndAnnotation...() 但它给出了上述异常/错误。

【问题讨论】:

    标签: java annotations automated-tests testng testrail


    【解决方案1】:

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

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

    【讨论】:

    • 我用过“方法方法 = result.getMethod().getConstructorOrMethod().getMethod();”这对我有用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多