If I have the following code:
[TestFixture] public class MyBaseTest { protected ISessionManager _sessionManager; [SetUp] public void SetUp() { /* some code that initializes _sessionManager */ } } [TestFixture] public class MyDerivedTest : MyBaseTest { IBlogRepository _repository; [SetUp] public void SetUp() { /* some code that initializes _repository */ } [Test] public void BlogRepository_TestGoesHere() { /* some tests */ } }
... NUnit does not call the base procedure SetUp. This is expected, and I have no problem with this on my own. I can get the resulting SetUp to call the base SetUp first, for example:
[TestFixture] public class MyDerivedTest : MyBaseTest { IBlogRepository _repository; [SetUp] public new void SetUp() { base.SetUp(); /* some code that initializes _repository */ }
It's not beautiful. If it were a constructor, I would not have to.
I could use a template template template and have the following:
public void MyBaseTest { abstract void SetUp(); [SetUp] public void BaseSetUp() { /* base initialization */ SetUp(); // virtual call } }
I do not particularly like this.
What do you do when their test classes require SetUp and they are derived from another class that also needs SetUp?
nunit
Roger Lipscombe
source share