2010-08-24 33 views
4

是否有可能在一个灯具中有多个[SetupTest]?不同配置的多个[SetupTest]

我正在使用Selenium和nUnit,并希望能够指定用户想要测试的浏览器。

我有一个简单的用户GUI来选择测试运行,但是,我知道将来我们希望将它挂钩到巡航控制以自动运行测试。理想情况下,我想要在我们的GUI和NUnit GUI上运行的测试。

回答

0

我怀疑你可以使用NUnit 2.5中引入的参数化测试来做你想做的事情,但我并不完全清楚你想在这里做什么。但是,你可以定义夹具,并将它带在其构造一个浏览器变量,然后使用参数化的TestFixture属性,如

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

NUnit Documentation获取更多信息。

Setup属性标识在每次测试之前运行的方法。只有每个测试夹具有一个安装程序才有意义 - 在每次测试运行之前将其视为“重置”或“准备”。

6

是否有可能在夹具中有多个[SetupTest]?编号

可以在基类中定义所有测试,让多个Fixture继承测试,然后在运行时选择一个与环境相关的fixture类型。

下面是我用于[TestFixtureSetup]的库存示例。相同的原理适用于所有设置属性。请注意,我只将[TestFixture]放在子类上。由于基础“TestClass”没有完整的设置代码,因此不需要直接运行测试。

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