Legacy Documentation. View NUnit 3 Documentation

SetUpFixtureAttribute (NUnit 2.4)

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.

There are a few restrictions on a class that is used as a setup fixture.

  • It must be a publicly exported type or NUnit will not see it.
  • It must have a default constructor or NUnit will not be able to construct it.

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. In the examples below, the method RunBeforeAnyTests() is called before any tests or setup methods in the NUnit.Tests namespace. The method RunAfterAnyTests() is called after all the tests in the namespace as well as their individual or fixture teardowns have completed exection.

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

Example:

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [SetUpFixture]
  public class MySetUpClass
  {
    [SetUp]
	RunBeforeAnyTests()
	{
	  // ...
	}

    [TearDown]
	RunAfterAnyTests()
	{
	  // ...
	}
  }
}
Imports System
Imports Nunit.Framework

Namespace Nunit.Tests

  <TestFixture()> Public Class MySetUpClass
    <SetUp()> Public Sub RunBeforeAnyTests()
	  ' ...
	End Sub

	<TearDown()> Public Sub RunAfterAnyTests()
	  ' ...
	End Sub
  End Class
End Namespace
#using <Nunit.Framework.dll>
using namespace System;
using namespace NUnit::Framework;

namespace NUnitTests
{
  [TestFixture]
  public __gc class MySetUpClass
  {
    [SetUp] public void RunBeforeAnyTests();
	[TearDown] public void RunAfterAnyTests();
  };
}

#include "cppsample.h"

namespace NUnitTests {
  // ...
}
package NUnit.Tests;

import System.*;
import NUnit.Framework.TestFixture;


/** @attribute NUnit.Framework.TestFixture() */
public class MySetUpClass
{
  /** @attribute NUnit.Framework.SetUp() */
  public void RunBeforeAnyTests()
  { /* ... */ }

  /** @attribute NUnit.Framework.TearDown() */
  public void RunAfterAnyTests()
  { /* ... */ }
}

See also...