Working with inherited fixtures and generic type restrictions
I'm am trying to test a hierarchy of classes, the first few layers of which are abstract. For example:
abstract Animal
-->absract Mammal
------>concrete Human
my approach when testing this sort of hierarchy is to use a test fixture for each tier:
AnimalTests
--->MammalTests
-------
This way I can keep the structure of the tests the same as the real code, and I can share the base class tests when I create new concrete types, i.e. Elephant also inherits Mammal and so ElephantTests inherits MammalTests, all the mammal tests are now applied to both humans and elephants.
I use generics on the test classes to allow me to call the extended methods on my test subject easily, so:
AnimalTests(of AnimalType as Animal)
--->MammalTests(of AnimalType as Mammal)
-------
This way each tier can call the methods it knows about on the test object. The two classes testing an abstract class have setups that will create a mock of the type, so AnimalTests creates a mock of Animal and MammalTests create a mock of Mammal. This behaviour is overriden by each class so when running HumanTests every tier gets a real Human to test.
This is nice in theory. But ...
To make all of this work I need to have my TestFixture attributes provide the correct type for the abstract tiers. So:
<TestFixture(
Class AnimalTests(of AnimalType as Animal)
But when I get to MammalTests adding the following:
<TestFixture(
Class MammalTests(of AnimalType as Mammal)
It breaks because of the way Nunit applies test fixtures, it will try and run the base (AnimalTests) fixture against the MammalTests class, which will fail as the Animal class does not pass the constraint As Mammal on the MammalTests class.
I understand why the TestFixtures are applied like this but what I would ideally like is a way to stop it applying the base classes attributes to the inherited classes. The workaround I have found is that I can create my classes without the testfixture attribute and then inherit from them to apply the testfixture:
AnimalTests(of AnimalType as Animal)
---><TestFixture()>
---->AnimalTest
This works, but causes me a problem with a tool I use for running tests, TestDriven.Net (http://
This is all very frustrating.
TL;DR:
Can we have some way of stopping the TestFixture attributes being inherrited so we can restrict the generic type args as we go further from the base class? Or is there something I have missed that will let me have the TestFixture on each of my classes with tests in,and allow me to test the various tiers in my hierarchy separately and together.
Question information
- Language:
- English Edit question
- Status:
- Answered
- For:
- NUnit V2 Edit question
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Can you help with this problem?
Provide an answer of your own, or ask Matthew Kubicki for more information if necessary.