获取和使用 TestNG @test 注释参数

getting and using TestNG @test annotation parameters

我想使用 @BeforeMethod 中的 @Test 注释中指定的 testName、suiteName 和描述,但无法在任何地方找到这样做的方法。非常感谢您的帮助。

@BeforeMethod
public void beforeMethod() throws Exception {
    initTest(testName here, suiteName here, description here);
}

@Test(testName = "Test Name", suiteName = "Suite Name", description = "Description")
public void Test01() throws Exception {
    //test code
}

我试试这个

@Test(testName = "my test", suiteName = "my suite", description = "my description")
public void test(ITestContext tContext){
    System.out.println(tContext.getName());
    System.out.println(tContext.getSuite().getName());
}

但由于某些原因,此 returns 默认值。但是下面(不是很优雅的方式)正在工作:

@BeforeMethod
public void printData(Method method){
    System.out.println(method.getAnnotation(Test.class).testName());
    System.out.println(method.getAnnotation(Test.class).suiteName());
    System.out.println(method.getAnnotation(Test.class).description());
}

@Test(testName = "my test", suiteName = "my suite", description = "my description")
public void test(){
    // Test code
}

以便您可以在 @BeforeMethod 中提取所需的值并在测试中使用它们。