I have a base class with a fair amount of "meta programming" to give it the flexibility/abstraction it needs to be rather generic.

I do have a lot of subclasses using the common methods in the base class, and I have behavior oriented unit tests covering all of the cases in each subclass.

Is it ok to skip testing the base class?

  • 1
    One thing I sometimes do when I start asking myself this question is try to break the code. Throw invalid input at it, specify wrong config values, etc.
    – Zymus
    Jul 30, 2015 at 1:22

2 Answers 2


To verify if you have enough tests or not, you can check your code coverage and your branch coverage induced by the tests (maybe by using a coverage tool, maybe manually by reviewing the code paths or by using a debugger).

If you come to the conclusion the tests for the subclasses give you a high enough coverage for your base classes code, then adding further tests obviously won't bring you much benefit. On the other hand, if there are code paths you can only test by adding specific tests using the base class directly, then you should go this route.

Another possible reason for "testing your base class directly" is that you want to test a specific function of that class "in isolation". Sometimes it can be easier to design test cases directly for a specific method, instead of only testing that method indirectly by calling the methods of your subclasses which use that method.

Note that when you have a generic base class for which the typical usage scenario is to derive a subclass, your base class is probably abstract. So for testing such a class you need to make a derivation anyway. For this situation, testing "the base class directly" could mean to add a special derivation just for testing purposes, of course.

  • Yes, I agree with direct unit tests on the base class for the more tangibly defined methods. What I didn't want to do is test all of the meta-programming since the unit tests on the subclasses implicitly test that stuff.
    – Nathan
    Jul 29, 2015 at 17:19
  • 4
    Designing a class that extends the base/abstract class that only exists within the test directory and serves only to expose / instantiate the base class is a perfectly legitimate and reasonable approach to getting the appropriate coverage of the base class and assurances that the underlying methods are working correctly in isolation of those more complex classes that are used.
    – user40980
    Jul 29, 2015 at 20:50
  • @MichaelT: yep. Does my answer sound like I have a different opinion about that?
    – Doc Brown
    Jul 29, 2015 at 20:59
  • @DocBrown not at all - you clearly articulated that point. I was just adding additional explanation that I would have written in my own answer (if you hadn't already stated that). I don't want to write an answer that would have started out "I agree with everything Doc said, but want to specifically call out and write more about..."
    – user40980
    Jul 29, 2015 at 21:40

Automated test were created for the programmers benefit, programmers were not created to serve the tests.

We have tests to make us more productive and to reduce the number of “issues” customers complain about.

So given that you have behaviour oriented unit tests covering all of the cases in each subclass, there is no benefit to the customer of you also testing the base class directly. This can be confirmed by changing some code in the base class and seeing if a test fails, e.g. common out a line of code, or adding a “not” to a “if statement”. (Just checking that every line of code is covered by the tests is not good enough.)

We are then left with asking if the test would make you more productive. This can happen in a few ways.

  • Letting you think clearer about the code - as the code is written this is unlikely in this case.
  • Letting you test some code in the base class in isolation, so debug it quicker, once again unlikely in the case as the code is already working.
  • Letting you change the code in the base class without having to understand the details of the logic in the subclasses – maybe if you are refactoring the code???

So given that the sub classes are working, I don’t see any point in writing direct tests for the base class. This is not to day, that there would not have been benefits to writing these tests at a early state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.