views:

378

answers:

5

Let's say I have a test class called testFixtureA with serveral 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?

+3  A: 

ignoring the whole base class:

@Ignore
class BaseClass {
   // ...
}

check out this example

dfa
This will ignore the base class everywhere, making it basically useless.
Willi
inherited test methods will be executed as tests in the derived classes...
dfa
+9  A: 

Restructure your test classes.

  • If you don't want to use the tests from the baseclass, then don't extend it
  • If you need other functionality from the base class, split that class in two - the tests, and the other functionality
Bozho
A: 

In the latest JUnit you can use the @Rule annotation on the subclass to inspect the test name and intercept the test run to ignore the test dynamically. But I would suggest that @Bozho's idea is the better one - the fact that you need to do this indicates a bigger problem that probably shows inheritance is not the right solution here.

Yishai
+3  A: 

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() {}
}
Matthew Flynn
Easy and obvious solution +1
Willi
A: 

I know, it's not the answer...

Consider the reason why you extend concrete test classes. You do duplicate test methods that way.

If you share code between tests then consider writing base test classes with helper and fixture setup methods or test helper class.

If for running tests then try organizing tests with suites and categories.

grigory