Skip to content

SetUp and TearDown

Charlie Poole edited this page Sep 1, 2017 · 1 revision

Attribute Usage

Attribute Usage by Fixture Type

TestFixture SetUpFixture
OneTimeSetUp Supported Supported
OneTimeTearDown Supported Supported
TestFixtureSetUp Deprecated Not Allowed
TestFixtureTearDown Deprecated Not Allowed
SetUp Supported Not Allowed
TearDown Supported Not Allowed

How Setup and TearDown Methods Are Called

Multiple SetUp, OneTimeSetUp, TearDown and OneTimeTearDown methods may exist within a class.

Setup methods (both types) are called on base classes first, then on derived classes. If any setup method throws an exception, no further setups are called.

Teardown methods (again, both types) are called on derived classes first, then on the base class. The teardown methods at any level in the inheritance hierarchy will be called only if a setup method at the same level was called. The following example is illustrates the difference.

public class BaseClass
{
   [SetUp]
   public void BaseSetUp() { ... } // Exception thrown!

   [TearDown]
   public void BaseTearDown() { ... }
}

[TestFixture]
public class DerivedClass : BaseClass
{
   [SetUp]
   public void DerivedSetUp() { ... }

   [TearDown]
   public void DerivedTearDown() { ... }

   [Test]
   public void TestMethod() { ... }
}

Execution will proceed as follows:

  • BaseSetUp
  • BaseTearDown

rather than

  • BaseSetUp
  • DerivedTearDown
  • BaseTearDown

See also: SetUp and TearDown Changes

Clone this wiki locally
You can’t perform that action at this time.