49

I'm looking at some test code using NUnit, which inherits from a base class containing a [SetUp] attribute:

public class BaseClass
{
   [SetUp]
   public void SetUp()
   {
     //do something
   }

}

[TestFixture]
public class DerivedClass : BaseClass
{
  [SetUp]
  public void SetUp()
  {

   //do something else, with no call to base.SetUp()
  }
   //tests run down here.
   //[Test]
   //[Test]
   //etc
}

The derived class will certainly need the work done in the base class' SetUp() method.

Am I missing something, or will the SetUp() method in the base class not be called when the derived class's tests are run? Is there something special with the [SetUp] attribute that ensures one will be called before the other?

vcsjones
  • 138,677
  • 31
  • 291
  • 286
larryq
  • 15,713
  • 38
  • 121
  • 190
  • Just another hint for anybody struggling with this issue: Make sure your `SetUp` methods are public. R# doesn't warn you if they are private but they won't run. – lex82 Mar 17 '16 at 09:59
  • Up-to-date answer for NUnit 2.5+ here: http://stackoverflow.com/a/22099351/532647 – Iarek Mar 23 '17 at 11:09
  • Constructors are your friends. If you want an additive setup behavior - use the constructors, as their syntax is more intuitive for this. Also, you should also consider the rationale in http://jamesnewkirk.typepad.com/posts/2007/09/why-you-should-.html – Arielr Jul 15 '13 at 16:43

2 Answers2

87

Before NUnit 2.5 the previous answers were correct; you could only have a single [SetUp] attribute for a test.

With NUnit 2.5 onwards you can have multiple methods decorated with the [SetUp] attribute. Therefore the below is perfectly valid in NUnit 2.5+:

public abstract class BaseClass
{
    [SetUp]
    public void BaseSetUp()
    {
        Debug.WriteLine("BaseSetUp Called")
    }
}

[TestFixture]
public class DerivedClass : BaseClass
{
    [SetUp]
    public void DerivedSetup()
    {
        Debug.WriteLine("DerivedSetup Called")  
    }

    [Test]
    public void SampleTest()
    {
        /* Will output
         *    BaseSetUp Called
         *    DerivedSetup Called
        */
    }
}

When inheriting NUnit will always run the '[SetUp]' method in the base class first. If multiple [SetUp] methods are declared in a single class NUnit cannot guarantee the order of execution.

See here for further information.

Luke Merrett
  • 5,724
  • 8
  • 38
  • 70
37

You can only have one SetUp method.

A TestFixture can have only one SetUp method. If more than one is defined the TestFixture will compile successfully, but its tests will not run.

http://www.nunit.org/index.php?p=setup&r=2.2.10

If you need to add additional setup logic in a child class, mark SetUp as virtual in your parent class, override it, and call base.SetUp() if you want the base class's setup to run, too.

public class BaseClass
{
   [SetUp]
   public virtual void SetUp()
   {
     //do something
   }

}



[TestFixture]
public class DerivedClass : BaseClass
{
  public override void SetUp()
  {
   base.SetUp(); //Call this when you want the parent class's SetUp to run, or omit it all together if you don't want it.
   //do something else, with no call to base.SetUp()
  }
   //tests run down here.
   //[Test]
   //[Test]
   //etc
}
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Thanks. So it's the case that the code above is incorrect, and the base SetUp() method will not be called? – larryq Jul 15 '13 at 19:59
  • @larryq the code in your question is invalid because you have two methods in the inheritance chain with a [SetUp] attribute. The fix is to only have one method with SetUp and then override it as needed. – vcsjones Jul 15 '13 at 20:20
  • That's what I thought, thank you. (The code above was pasted from a project I've been working on, and I wondered if I'd missed something.) – larryq Jul 16 '13 at 14:48
  • 7
    This is not valid for nUnit >= 2.5, see https://stackoverflow.com/a/22099351/2881450 – jHilscher Dec 06 '17 at 10:37