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>
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? –
grazie matt. Vedi le risposte di cedro sotto per maggiori dettagli. – Afamee