使用testnglistners在运行时为@test方法设置注解

slwdgvem  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(313)

我正在使用testng编写诱惑报告,并且有一个非常基本的方法,如下所示。

@Test
@DisplayName("Hello this is a description for allure report")
public void method1(){
  System.out.println("Hi i am from method1");
}

在上面的代码中,@displayname来自allure report。现在我试图实现的是使用testnglistners在运行时设置displayname。
我查看了itextresult和itestcontext,但据我所知,它不支持这一点。
有没有其他方法可以达到这个目的?

qq24tv8q

qq24tv8q1#

如果您使用的是junit启动器,那么只需迭代测试计划就可以获得显示名称。
显示名称的代码如下:

testPlan.getRoots ()
            .forEach (testIdentifier -> testPlan.getChildren (testIdentifier).
                    forEach (testIdentifier1 -> testPlan.getChildren (testIdentifier1).
                            forEach (testIdentifier2 -> testCaseList.add (testIdentifier2.getDisplayName ()))));

相关问题