Legacy Documentation. View NUnit 3 Documentation

SetUpAttribute (NUnit 2.0)

This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. 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.

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

  [TestFixture]
  public class SuccessTests
  {
    [SetUp] public void Init()
    { /* ... */ }

    [TearDown] public void Cleanup()
    { /* ... */ }

    [Test] public void Add()
    { /* ... */ }
  }
}
Imports System
Imports Nunit.Framework

Namespace Nunit.Tests

  <TestFixture()> Public Class SuccessTests
    <SetUp()> Public Sub Init()
    ' ...
    End Sub

    <TearDown()> Public Sub Cleanup()
    ' ...
    End Sub

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

namespace NUnitTests
{
  [TestFixture]
  public __gc class SuccessTests
  {
    [SetUp] void Init();
    [TearDown] void Cleanup();

    [Test] void Add();
  };
}

#include "cppsample.h"

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

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


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

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

  /** @attribute NUnit.Framework.Test() */
  public void Add()
  { /* ... */ }
}

SetUp Inheritance

The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method will be called before each test method in the derived class. If you wish to add more SetUp functionality in a derived class you need to mark the method with the appropriate attribute and then call the base class method.

See also...