I'm using TestNG framework, and many test classes extends Base abstract Test providing some additional information.
I want to set @Test(enabled = false)
on whole test class, which is problematic in TestNG as any @Test
annotation on method will override first one. Which means, even disabled class, all methods will run anyway.
I found workaround provided by framework authors in pull requests #816 - add listener, that modifies @Test
annotations on methods if class that they are defined have @Test(enabled = false)
:
public class TestClassDisabler implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,
Method testMethod) {
if (testMethod != null) {
Test test = testMethod.getDeclaringClass().getAnnotation(Test.class);
if (test != null && !test.enabled()) {
annotation.setEnabled(false);
}
}
}
}
It works like a charm, however only for tests that don't use inheritance. For this scenario, testMethod.getDeclaringClass()
from listener returns original class in which method was declared, not class of object instance that started method.
@Test(enabled = false)
class SpecificTest extends BaseTest {
@Test
public void testSomething() {}
}
abstract class BaseTest {
@Test
public void veryGenericTest() {}
}
For this example, only testSomething
is disabled, veryGenericTest
still runs.
Aucun commentaire:
Enregistrer un commentaire