| || |
C# Abstract Classes
Does anyone have insight on testing C# abstract classes?
In particuliar, how to test methods that are not exposed like traditional public methods.
Re: C# Abstract Classes
Well, an abstract class is a class that has no direct instances, but whose descendants may have direct instances. Such classes are normally used as base-classes in inheritance hierarchies. So it should be relatively obvious of a few things you can test in terms of using the abstract classes as your drivers, so to speak. An abstract class is generally there to give a templatized structure that specifies a certain type of behavior that must be implemented in a subclass or a set of subclasses. So, by that logic, what to test is obvious: test the subclass and make sure that blueprint is being adhered to.
You might also check out Testing Interface Compliance with Abstract Test.
Also check out: Abstract Classes.
You might also check out IBM's article on Mock Object Testing. While that does not directly apply to your question, there is enough there that you should be able to see as relevant. (Note: there are some downsides to the IBM approach.)
Note that in terms of abstract methods, the abstract method declaration is permitted to override a virtual method, if such are used. This allows an abstract class to force the re-implementation of the method in any derived classes and that obviously makes the original implementation of the method unavailable. That is something you can test for. Also keep in mind that abstract classes do not provide zero implementation - at least not necessarily. Rather they provide partial implementation. Practically speaking, that means another class can inherit from an abstract class and can create their instances from that. So there is another means of testing. Also note that an abstract class can contain both abstract and non-abstract methods. When a given class inherits from an abstract class, the derived class must implement all of the abstract methods declared in the base class. An abstract class can also implement from an interface and if you are doing that you might have some other concerns.
Keep in mind that some unit test suites out there use abstract classes and methods as the basis for the internal unit testing as well, so that is something to keep in mind. (This is done with Java and with C#, so far as I know.)
Re: C# Abstract Classes
We use Segue's .NET explorer which a free download. It lets us create tests for our different .NET components. You can check it out here.