views:

45

answers:

2

Hi all

I have 2 questions about functionality of nunit.

What is the difference between [TestFixtureSetUp] and [SetUp] attributes ?

I am writing a some class with tests and I see that half of my test functions need one setup, And an another half needs another set up. How can I have in one class two little different SetUp functions that are called with different functions

Thanks.

+4  A: 

Method marked with [TestFixtureSetUp] attribute will be executed once before all tests in current test suite, and method marked with [SetUp] attribute will be executed before each test.

As for class with tests which contains tests requring different set up functions. Just split this class in two - each one with it's own SetUp function.

    [TestFixture]
    public void TestSuite1
    {
      [SetUp]
      public void SetUp1()
      {
        ...
      }

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

   [TestFixture]
    public void TestSuite2
    {
      [SetUp]
      public void SetUp2()
      {
        ...
      }

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

or call SetUp function explicitly

    [TestFixture]
    public void TestSuite
    {

      public void SetUp1()
      {
        ...
      }

      public void SetUp2()
      {
        ...
      }

      [Test]
      public void Test1()
      {
        SetUp1();

        ...
      }

      [Test]
      public void Test2()
      {
        SetUp2();

        ...
      }
    }
Yauheni Sivukha
A: 

A TestFixtureSetup method is executed once before any of the test methods are executed. A Setup method is executed before the execution of each test method in the test fixture.

How can I have in one class two little different SetUp functions that are called with different functions

You can't have two different SetUp functions in a single class marked as TestFixture. If individual tests need some initialization then it makes sense to put the initialization code inside those function themselves.

I see that half of my test functions need one setup

I think then you need to factor out the tests...

amit.codename13