开发者

Multiple [SetupTest] for different configs

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 u开发者_Go百科p to cruise control to run the tests automatically. Ideally I want tests that can be run on both our GUI and the NUnit GUI.


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...
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜