使用testngListners

问题描述

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

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

在上面的代码中,@ displayName来自诱惑报告。现在,我想要实现的是在运行时使用TestngListners设置displayName。

我查看了ITextResult和ITestContext,但是据我所知,它对此不提供支持

还有其他方法可以实现吗?

解决方法

如果您使用的是Junit启动器,则只需遍历测试计划即可获得显示名称。

显示名称的代码为:

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