Hay dos maneras que yo sepa que permite el control de la "desactivación" pruebas en TestNG.
La diferenciación que es muy importante tener en cuenta es que SkipException saldrá de todas las pruebas posteriores mientras que la implementación de IAnnotionTransformer usa Reflection para deshabilitar las pruebas individuales, según una condición que usted especifique. Explicaré tanto SkipException como IAnnotationTransfomer.
ejemplo SKIP Excepción
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 ejemplo
Un poco más complicado, pero la idea detrás de esto es un concepto conocido como reflexión.
Wiki - http://en.wikipedia.org/wiki/Reflection_(computer_programming)
Primera implementar la interfaz IAnnotation, guardar esto en un archivo * .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()){
}
Luego se prueba en el archivo Java suite de hacer lo siguiente en la función @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 último paso es asegurarse de que agregue un oyente en el archivo build.xml. La mina terminó luciendo así, esta es solo una línea de la construcción.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>
anotaciones no son código ejecutable, así que esto es poco probable. ¿Qué estás realmente tratando de hacer? ¿En qué condiciones quieres que se ejecute o no una prueba? –
gracias matt. Consulte la respuesta de cedrics a continuación para obtener más detalles. – Afamee