看看文档中的
dependency injection部分.它指出,在这种情况下可以使用依赖注入:
Any @BeforeMethod (and @AfterMethod) can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this @BeforeMethod finishes (or after the method as run for @AfterMethod).
所以基本上你只需要在你的@BeforeMethod中声明一个类型为java.lang.reflect.Method的参数,你将可以访问以下测试名称的名字.就像是:
@BeforeMethod
protected void startTest(Method method) throws Exception {
String testName = method.getName();
System.out.println("Executing test: " + testName);
}
还有一种使用ITestNGMethod界面(doc)的方式,但是我不太清楚如何使用它,如果你有兴趣,我可以看看它.