So, I’m kind of wondering if something like this exists. I currently have a set of tests that share common set up code which I only want to execute once for all of the tests, as well as some set up code that I want executed before each test. In addition, though, there are sets of tests that share test set up that I want to execute only once for that set of tests. Basically here’s what I want to happen
Parent Fixture Set Up Child Fixture Set Up Parent Set Up Child Set Up Test Child Tear Down Parent Tear Down Parent Set Up Child Set Up Test Child Tear Down Parent Tear Down (Etc…) Child Fixture Tear Down Child Fixture Set Up (Etc…) Child Fixture Tear Down Parent Fixture Tear Town
There’s two ways I can see of doing this. One: through inheritance. Basically, the parent / child relationship is expressed through a base / derived relationship. NUnit may support this, but I haven’t tried it. I’m not really sure what happens when you provide two SetUp attributes for one class to NUnit (even if they are a base / derived relationship)
Second, and this is the method I would like to use, you could do it through a containment system. The parent contains the child, like so:
[TestFixture] public class ParentFixture { [FixtureSetUp] public void FixtureSetUp() { } [FixtureTearDown] public void FixtureTearDown() { } [TestFixture] public class ChildFixture { // etc. } }
The only problem there is that in order to access, say, private data or methods of the parent class, you need to pass the parent class in as a parameter to the child. I’m pretty sure this is not supported by NUnit.
Does anyone know of an addin that might do this? Has anyone had a similar use case for any unit testing environment? How do you get around it?