针对不同配置的多个 [SetupTest]

发布于 2024-09-15 23:18:59 字数 183 浏览 3 评论 0原文

一个装置中是否可以有多个 [SetupTest]?

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-09-22 23:18:59

一个装置中是否可以有多个 [SetupTest]?不。

可以在基类中定义所有测试,让多个固定装置继承测试,然后在运行时选择依赖于环境的固定装置类型。

这是我为 [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...
    }
}

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...
    }
}
救赎№ 2024-09-22 23:18:59

我怀疑您可以使用 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 文档 了解更多信息。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文