2011-12-12 5 views

risposta

13

Dai un'occhiata alla sezione dependency injection nella documentazione. Si afferma che la dipendenza iniezione può essere utilizzato ad esempio in questo caso:

Qualunque @BeforeMethod (e @AfterMethod) può dichiarare un parametro di tipo java.lang.reflect.Method. Questo parametro riceverà il metodo di prova che verrà chiamato una volta terminato questo @BeforeMethod (o dopo il metodo come eseguito per @AfterMethod).

Quindi, in pratica non resta che dichiarare un parametro di tipo java.lang.reflect.Method nel vostro @BeforeMethod e si avrà accesso al nome dei seguenti nome del test. Qualcosa di simile:

@BeforeMethod 
protected void startTest(Method method) throws Exception { 
    String testName = method.getName(); 
    System.out.println("Executing test: " + testName); 
} 

C'è anche un modo utilizzando l'interfaccia ITestNGMethod (documentation), ma come io non sono esattamente sicuro su come usarlo, mi limiterò a farvi avere un'occhiata se si sei interessato.

+0

Io corro la mia testcase utilizzando i dati forniscono set di dati multiple, così nel rapporto misura in cui sta mostrando lo stesso metodo è in esecuzione più volte tante volte quanti più dati abbiamo nel foglio excel, quindi voglio passare il nome del testcase come variabile (That's in excel) a prima del metodo che è in classe AbstractBaseTestCase, c'è un modo per raggiungere questo? –

1

Sotto esempio spiega come è possibile ottenere il nome del metodo e il nome della classe nella prima Metodo

@BeforeMethod 
     public void beforemethod(Method method){ 
//if you want to get the class name in before method 
     String classname = getClass().getSimpleName(); 
//IF you want to get the method name in the before method 
     String methodName = method.getName()  
     } 

@Test 
public void exampleTest(){ 


}