tags:

views:

28

answers:

2

Is it possible to have multiple [SetupTest]'s in a fixture?

I am using Selenium and nUnit and would like to be able to specify the Browser on which the user wants to test.

I have a simple user GUI for selecting tests to run but, I am aware that in the future we want to hook this up to cruise control to run the tests automatically. Ideally I want tests that can be run on both our GUI and the NUnit GUI.

A: 

I suspect you can use the parameterized tests introduced in NUnit 2.5 to do what you want, but I'm not totally clear what you want to do here. However, you could define the fixture and have it take a Browser variable in its constructor, then use parameterized TestFixture attributes like

TextFixture["Firefox"]
TestFixture["Chrome"]
public class ParameterizedTestFixture { 
  //Constructor
  public ParameterizedTestFixture( string Browser) {
  //set fixture variables relating to browser treatment
  }
  //rest of class
}

See NUnit Documentation for more info.

The Setup attribute identifies a method that is run before each test. It only makes sense to have one Setup per test fixture - think of it as a 'reset' or 'preparation' before each test is run.

imoatama
+1  A: 

Is it possible to have multiple [SetupTest]'s in a fixture? No.

It is possible to define all your tests in a base class, have multiple fixtures inherit the tests, and then select an environment dependent fixture type at runtime.

Here’s the stock example that I have for [TestFixtureSetup]. The same principle works for all the setup attributes. Notice that I’m only putting [TestFixture] on the child classes. Since the base “TestClass” doesn’t have complete setup code, you don’t want to run the tests directly.

public class TestClass
{
    public virtual void TestFixtureSetUp()
    {
        // environment independent code...
    }

    [Test]
    public void Test1() { Console.WriteLine("Test1 pass.");  }

    // More Environment independent tests...
}

[TestFixture]
public class BrowserFixture : TestClass
{
    [TestFixtureSetUp]
    public override void TestFixtureSetUp()
    {
        base.TestFixtureSetUp();
        // environment dependent code...
    }
}

[TestFixture]
public class GUIFixture : TestClass
{
    [TestFixtureSetUp]
    public override void TestFixtureSetUp()
    {
        base.TestFixtureSetUp();
        // environment dependent code...
    }
}
ErnieL