NUnit and [SetUp] in base classes

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)