SetUp in derived classes using NUnit?

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?

+7
nunit
source share
5 answers

You must call the method directly.

  [SetUp] public void DerivedSetUp() { base.BaseSetUp(); // Do something else } 

Edit: I have not tried, but maybe a partial method may work as well. I would rather do the above though.

Edit2: I was just trying to use partial methods. This did not work. Even if that were the case, I think it will still be easier to name the base class.

+9
source share

You have a base class explicitly. Given that NUnit uses the [Setup] attribute to flag a test setup, I find this to be the β€œright thing” for NUnit because it complies with the usual language rules.

Of course, NUnit can search for base classes and automatically call their installation functions, but I think that would be rather unexpected for most people.

However, there is at least one unit testing environment that uses constructors for installation: xUnit.Net . Here, the base class setting is called automatically, since this is the behavior of the designers in C #.

(Note that xUnit.Net recommends using the test setup again.)

+3
source share

It looks like you want the installation to run once before starting any test, and not once before starting each test. The [SetUp] annotations cause the method to run once before each test in your device. [SetUp] is not inherited.

The annotations you want to use is [TestFixtureSetUp], which runs only once before running any tests in the instrument. This is inherited as you expected. :)

See TextFixtureSetUp docs

+1
source share

The approach I used that I learned from TDD FireStarter in Tampa was to have a setup method in the base class, and then have a virtual method in the base class called observation. This method is then called in the base class at the end of the installation method.

Then what you do is in a new class that comes from the base class, you will override the observation method. The trick in this scenario is that you want to execute the installation method of the base class, and the child class does not have the installation method. The reason for this is the code that you have in the observation method - these are only those additional things that are needed for a test class for children.

This approach works well for me, but one of them is that the test runner wants to run tests of the base class, so I do this to move the tests from the base class to a new class that outputs from the base, if I have one.

0
source share

The following works in MbUnit. It can work in NUnit.

 [TestFixture] public abstract class Base { [SetUp] public virtual void SetUp() { //Some stuff. } } public class Derived : Base { public override void SetUp() { base.SetUp(); //Some more stuff. } [Test] public virtual void Object_WhenInACertainState_WhenAMethodIsCalled_Throws() { //Create and set state on the object. //Call the method. //Assert the method threw. } } 
-one
source share

All Articles