How to test the behavior of the implementations of interface methods in (abstract) classes without having to copy the tests to each class?
I have (abstract) classes that implement multiple interfaces. I know how each interface should behave, and I define this in test methods so that I don't have to manually repeat these tests for each and every implementation of an interface.
I could create for each interface an abstract class with the tests, and have an abstract method CreateSUT()
that creates a new instance of the concrete class. But then I'd have to create a new class with the same CreateSUT()
implementation for each interface a class implements, as C# does not support multiple inheritance. Is there a better way to do this?
Also note that I also want to test interfaces implemented in abstract classes that have several non-abstract subclasses, complicating the matter slightly.
This question is not about whether I should unit test my interface implementations. Opinions differ and I've decided to do it because I know how the interface implementations are expected to behave (never returning a null value, returning a read-only collection, etc) and putting these tests together makes it much easier for me to test their implementations, however many there may be.