2010-10-15 15 views
14

Esiste attualmente un modo per disabilitare la prova TestNG base a una condizioneCome disattivare TestNG test basato su una condizione

So che è possibile testare attualmente disable come così in TestNG:

@Test(enabled=false, group={"blah"}) 
public void testCurrency(){ 
... 
} 

Mi piacerebbe disabilitare lo stesso test in base a una condizione ma non so come. Qualcosa di simile:

@Test(enabled={isUk() ? false : true), group={"blah"}) 
public void testCurrency(){ 
... 
} 

Chiunque ha un indizio su se questo è possibile o no.

+0

annotazioni non sono codice eseguibile, quindi questo è improbabile. Cosa stai cercando di fare davvero - in quali condizioni vorresti che un test venisse eseguito o meno? –

+0

grazie matt. Vedi le risposte di cedro sotto per maggiori dettagli. – Afamee

risposta

10

Sono disponibili due opzioni:

Il vostro trasformatore di annotazione testerebbe la condizione e quindi sovrascrivere l'annotazione @Test per aggiungere l'attributo "enabled = false" se la condizione non è soddisfatta.

+0

Grazie cedric. Penso che mi piacerebbe esplorare l'opzione 'annotation transformer'. sembra più come quello che sto cercando. – Afamee

+0

Grazie ancora. Non mi ci è voluto molto per ottenere un esempio funzionante di questo trasformatore. Una cosa però non si sta comportando come mi aspettavo. Voglio trasformare dinamicamente il nome del test che eseguo (... almeno il modo in cui verrà visualizzato sul risultato del test) chiamando annot.setTestName (concatString) dove annot rappresenta l'annotazione del metodo ma il risultato ritorna con l'originale nome invariato. C'è un altro modo per fare ciò?? Spero che non ti abbia confuso – Afamee

+0

Non sarà possibile eseguire l'override del comportamento di una classe in fase di esecuzione, è proprio come viene progettato Java. Invece, ti suggerisco di mettere la logica che decide quale nome è direttamente nel test in modo da poterlo restituire in getTestName(). –

30

Un'opzione più semplice consiste nell'utilizzare l'annotazione @BeforeMethod su un metodo che controlla le condizioni. Se si desidera saltare i test, quindi basta lanciare un SkipException. Come questo:

@BeforeMethod 
protected void checkEnvironment() { 
    if (!resourceAvailable) { 
    throw new SkipException("Skipping tests because resource was not available."); 
    } 
} 
+0

Salve, dal momento che il lancio di 6.13 SkipException imposta lo stato su Fallito invece di Saltato. Vedi https://stackoverflow.com/questions/20022288/conditional-skipping-of-testng-tests/47571922#47571922 –

5

Ci sono due modi che conosco che si permettono il controllo di "disabilitazione" test in TestNG.

La differenziazione che è molto importante notare è che SkipException interromperà tutti i test successivi durante l'impiantazione IAnnotationTransformer utilizza Reflection per annullare singoli test, in base a una condizione specificata. Spiegherò sia SkipException che IAnnotationTransfomer.

SKIP eccezione esempio

import org.testng.*; 
import org.testng.annotations.*; 

public class TestSuite 
{ 
    // You set this however you like. 
    boolean myCondition; 

    // Execute before each test is run 
    @BeforeMethod 
    public void before(Method methodName){ 
     // check condition, note once you condition is met the rest of the tests will be skipped as well 
     if(myCondition) 
      throw new SkipException(); 
    } 

    @Test(priority = 1) 
    public void test1(){} 

    @Test(priority = 2) 
    public void test2(){} 

    @Test(priority = 3) 
    public void test3(){} 
} 

IAnnotationTransformer esempio

Un po 'più complicato, ma l'idea è un concetto noto come riflessione.

Wiki - http://en.wikipedia.org/wiki/Reflection_(computer_programming)

Prima implementare l'interfaccia IAnnotation, salvare questo in un file * .java.

import java.lang.reflect.Constructor; 
import java.lang.reflect.Method; 
import org.testng.IAnnotationTransformer; 
import org.testng.annotations.ITestAnnotation; 

public class Transformer implements IAnnotationTransformer { 

// Do not worry about calling this method as testNG calls it behind the scenes before EVERY method (or test). 
// It will disable single tests, not the entire suite like SkipException 
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){ 

    // If we have chose not to run this test then disable it. 
    if (disableMe()){ 
     annotation.setEnabled(false); 
    } 
} 

// logic YOU control 
private boolean disableMe()){ 
} 

Poi si prova di file java suite di fare quanto segue nella funzione @BeforeClass

import org.testng.*; 
import org.testng.annotations.*; 

/* Execute before the tests run. */  
@BeforeClass 
public void before(){ 

    TestNG testNG = new TestNG(); 
    testNG.setAnnotationTransformer(new Transformer()); 
} 

@Test(priority = 1) 
public void test1(){} 

@Test(priority = 2) 
public void test2(){} 

@Test(priority = 3) 
public void test3(){} 

Un ultimo passo è quello di garantire che si aggiunge un ascoltatore nel file build.xml. Il mio ha finito per sembrare così, questa è solo una riga dalla build.xml:

<testng classpath="${test.classpath}:${build.dir}" outputdir="${report.dir}" 
    haltonfailure="false" useDefaultListeners="true" 
    listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,Transformer" 
    classpathref="reportnglibs"></testng> 
0

Lanciare un SkipException in un metodo annotato con @BeforeMethod non ha funzionato per me perché è saltato tutte le restanti prove della mia suite di test, senza riguardo se un SkipException sono stati gettati per questi test.

non ho indagare a fondo, ma ho trovato un altro modo: usando l'attributo dependsOnMethods sul @Test della nota:

import org.testng.SkipException; 
import org.testng.annotations.Test; 

public class MyTest { 

    private boolean conditionX = true; 
    private boolean conditionY = false; 

    @Test 
    public void isConditionX(){ 
    if(!conditionX){ 
     throw new SkipException("skipped because of X is false"); 
    } 
    } 

    @Test 
    public void isConditionY(){ 
    if(!conditionY){ 
     throw new SkipException("skipped because of Y is false"); 
    } 
    } 

    @Test(dependsOnMethods="isConditionX") 
    public void test1(){ 

    } 

    @Test(dependsOnMethods="isConditionY") 
    public void test2(){ 

    } 
}