Unit Testing Abstract classes

Opinions wanted: How do you write unit tests for abstract (MustInherit in VB.NET) classes? Do you derive a simple class off your abstract class in your unit test assembly? Or do you just write unit tests for the derived classes? Hmmm.....

5 Comments

  • I'm nerw to this test stuff...



    Wouldn't it make sense to have a test that tries to create an instance where failure is sucess because the class is abstract.

  • I usually create a concrete type for specifically for my unit test that adds basic behavior that allows me to accurately test the functionality of the abstract class.

  • AndrewSeven,



    My abstract class contains some functionality that I want to test. So I need to instantiate something to test it, but the class is abstract.



    I like Drew's suggestion. That's what I was leaning towards.

  • Ya, I thought of that, but if you create a class just to test the abstract class, shouldn't it be "in" the test, not in the project?



    Surely you have classes that inherit from the abstract class?

  • Why don't you create a mock object (in your test project) that inherits from you abstract base class. The abstract methods could return defaults (null for reference types, defaults for value types) if they expect a return value, or have an out (or ref) parameter. You probably will only be interested in testing the non-abstract methods, right?

Comments have been disabled for this content.