2017-02-07 58 views
0

我使用TestNGSelenium。在此,我使用@AfterMethod@Test注释。如何获取方法名称?

在我用过的方法@AfterMethod中,我需要的方法名称有@Test

对于前:

@TEST注释变量的方法名
@Test 
public void testmethod() { 
    System.out.println("test"); 
} 

@AfterMethod 
public void aftermethod() { 
    String methodnameofTESTANnnoation=....? 
    System.out.println([email protected]); 
} 

在这里,我需要@Test标注方法的名称即TestMethod的

+0

有与返回的值什么问题? make testmethod public String testmethod()并添加return(“testmethod”)例如然后尝试String methodnameofTESTANnation = testmethod(); – ReadyFreddy

+1

@shmosel问题是关于testng的。对于testng检查了这个 - http://stackoverflow.com/questions/2952202/how-do-i-get-the-name-of-the-test-method-that-was-run-in-a-testng-拆卸 - 甲基无定向= 1&lq = 1 – Grasshopper

+0

@草蜢良好的捕获。你的链接看起来像一个完美的副本。 – shmosel

回答

1

TestNG能够将Method引用注入到配置方法中。 从the documentation

不限@BeforeMethod(和@AfterMethod)可以声明 类型java.lang.reflect.Method中的参数。此参数将接收测试方法 ,在@BeforeMethod完成后(或在运行@AfterMethod的 方法之后)将调用该方法。

因此,与样品:

@Test 
public void testmethod(){ 
    System.out.println("test"); 
} 

@AfterMethod 
public void aftermethod(Method m){ 
    String methodNameOfTest = m.getName(); 
    System.out.println(methodNameOfTest); 
} 
+0

谢谢。这是工作... – Sandeep

+0

@Sandeep然后,接受答案;) – juherr