TestFixtureTearDownAttribute (NUnit 2.1 / 2.5)

This attribute is used inside a TestFixture to provide a single set of functions that are performed once after all tests are completed.

Before NUnit 2.5, a TestFixture could have only one SetUp method and it was required to be an instance method.

Beginning with NUnit 2.5, TestFixtureTearDown methods may be either static or instance methods and you may define more than one of them in a fixture. Normally, multiple TestFixtureTearDown methods are only defined at different levels of an inheritance hierarchy, as explained below.

So long as any TestFixtureSetUp method runs without error, the TestFixtureTearDown method is guaranteed to run. It will not run if a TestFixtureSetUp method fails or throws an exception.

Example:

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

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

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

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

Namespace Nunit.Tests

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

    <TestFixtureTearDown()> 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
  {
    [TestFixtureSetUp] void Init();
    [TestFixtureTearDown] 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.TestFixtureSetUp() */
  public void Init()
  { /* ... */ }

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

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

Inheritance

The TestFixtureTearDown attribute is inherited from any base class. Therefore, if a base class has defined a TestFixtureTearDown method, that method will be called after each test method in the derived class.

Before NUnit 2.5, you were permitted only one TestFixtureTearDown method. If you wanted to have some TestFixtureTearDown functionality in the base class and add more in the derived class you needed to call the base class method yourself.

With NUnit 2.5, you can achieve the same result by defining a TestFixtureTearDown method in the base class and another in the derived class. NUnit will call base class TestFixtureTearDown methods after those in the derived classes.

Note: Although it is possible to define multiple TestFixtureTearDown methods in the same class, you should rarely do so. Unlike methods defined in separate classes in the inheritance hierarchy, the order in which they are executed is not guaranteed.

See also...