自定义@Annotation 以在运行时报告布尔值

Custom @Annotation to report boolean at runtime

我正在四处寻找,看看我想要完成的事情是否可以通过注释实现。

基本上,我们有一堆我们正在微观管理的 TestNG 测试用例

示例:

@Test
public void reportingTest(){
 Assert.true(false);
}

以上会简单地失败,但我们将所有内容包装在断言 try catch 中。

@Test
public void reportingTest(){
 try {
    Assert.true(false);
    Report.batch(Enum.Pass, 106);
} catch (Throwable t) {
    Report.batch(Enum.Fail, 106, "Test case has failed");
    }
}

然而,在数百个测试用例之后...拥有 try catch 非常麻烦。

我正在努力完成这样的事情

@Reporting(id=106)
@Test
  public void reportingTest(){
     Assert.true(false);
 }

在注释内部,我将能够捕获失败的断言并根据我的 ID 发送注销。

谢谢!

TestNG 提供 listeners and the one you are looking for may be the TestListener.

您的注释将从那里可用:tr.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Reporting.class)