Let's say I have a test class called testFixtureA
with several methods testA
, testB
, testC
, etc, each with @Test
annotation.
Let's now say I subclass testFixtureA
into class called testFixtureAB
and I don't overwrite anything. testFixtureAB
is empty as for now.
When I run tests from testFixtureAB
, methods testA
, testB
and testC
are executed by test runner because test runner doesn't distinguish between test methods from class and baseclass.
How can I force test runner to leave out tests from baseclass?
and I don't overwrite anything. testFixtureAB is empty as for now
There's your answer. If you want to not run testB from the main class, overrride it:
public class testFixtureAB extends testFixtureA {
@Override
public void testB() {}
}
Restructure your test classes.
ignoring the whole base class:
@Ignore
class BaseClass {
// ...
}
check out this example
It's quite easy to achieve implementing some few classes:
TestRunner
@IgnoreInheritedTests
org.junit.runner.manipulation.Filter
On the filter class:
public class InheritedTestsFilter extends Filter {
@Override
public boolean shouldRun(Description description) {
Class<?> clazz = description.getTestClass();
String methodName = description.getMethodName();
if (clazz.isAnnotationPresent(IgnoreInheritedTests.class)) {
try {
return clazz.getDeclaredMethod(methodName) != null;
} catch (Exception e) {
return false;
}
}
return true;
}
@Override
public String describe() {
// TODO Auto-generated method stub
return null;
}
}
on your custom runner:
/**
* @param klass
* @throws InitializationError
* @since
*/
public CustomBaseRunner(Class<?> klass) throws InitializationError {
super(klass);
try {
this.filter(new InheritedTestsFilter());
} catch (NoTestsRemainException e) {
throw new IllegalStateException("class should contain at least one runnable test", e);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With