Testing Class Hierarchies

Should you test an abstract base class? When testing the subclass of an abstract base class, should you test the entire interface of the class or just the methods that changed in the subclass. Lets look at each one of the questions in detail.Should you test your abstract base classes?

If you are truly are doing Test Driven Development (TDD), you would never just write an abstract base class. The base class are extracted from concrete classes as apart of the red/green/refactor cycle. I never move tests down to test the abstract base class. I don’t see the point in testing the abstract base class, as they can not be used without creating a concrete class.

Should you test the entire interface of the class or just the methods that changed in the subclass?

I say you test the entire interface. The abstract base class is just an implementation detail of the subclass. The subclass must still conform to the contract of the entire interface. There may be some duplication of tests, but I think it is worth the extra effort to ensure that each of the subclasses adhere to the contract of the interface that they implement.

We had this exact discussion today at work. There were many different opinions on this subject. I would love to hear your thoughts on the subject. How would you answer the above questions?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: