Search code examples
unit-testingnunitmbunit

Looking for NUnit's equivalent of MbUnit's [AssemblyFixture]


I'm looking for the closest equivalent of using MbUnit's [AssemblyFixture] attribute in NUnit. I want my unit tests to run some setup only once (in total) before all fixtures of the namespace and to run some kind of cleanup once (in total) after all the fixtures of the namespace. Thank you.


Solution

  • The closest thing I'm aware of in NUnit is [SetUpFixture]. The NUnit documentation on [SetUpFixture] explains:

    This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace. The class may contain at most one method marked with the SetUpAttribute and one method marked with the TearDownAttribute.

    Further, it states:

    Only one SetUpFixture should be created in a given namespace. A SetUpFixture outside of any namespace provides SetUp and TearDown for the entire assembly.

    Regarding [SetUp] and [TearDown] methods inside [SetUpFixture]:

    The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.

    This is the example from the NUnit 2.6.3 documentation (although it was added in version 2.4):

    namespace NUnit.Tests
    {
      using System;
      using NUnit.Framework;
    
      [SetUpFixture]
      public class MySetUpClass
      {
        [SetUp]
        RunBeforeAnyTests()
        {
          // ...
        }
    
        [TearDown]
        RunAfterAnyTests()
        {
          // ...
        }
      }
    }