OneTimeSetUp仅为参数化测试夹具

时间:2017-07-07 16:52:37

标签: c# unit-testing nunit nunit-3.0

我创建了一个测试夹具,该夹具使用一对字符串值进行参数化,并包含多种测试方法。参数传递给测试类构造函数。我希望夹具的每个实例都使用一对特定的字符串参数创建,然后运行其中的所有测试。夹具中所有测试的常见初始化都是在用OneTimeSetUp修饰的方法中完成的,最终化在OneTimeTearDown中完成:

[TestFixtureSource(typeof(MyFixtureData), "FixtureParameters")]
public class MyModuleTests
{
private string parameter1;
private string parameter2;

public MyModuleTests(string parameter1, string parameter2)
{
    this.parameter1 = parameter1;
    this.paramter2 = paramter2;
}

[OneTimeSetUp]
public void InitializeFixture()
{
// perform initialization steps: create test objects, run browser, open test page, check that required objects exist on page etc.
}

[OneTimeTearDown]
public void FinalizeFixture()
{
    // perform finalization steps: close browser etc.
}

[Test]
public void MyModuleTest1()
{
    // assert something
}

[Test]
public void MyModuleTest2()
{
    // assert something
}

[Test]
public void MyModuleTest3()
{
    // assert something
}

public class MyFixtureData
{
    public static IEnumerable FixtureParameters
    {
        get
        {
            yield return new TestFixtureData("string value1a", "string value1b");
            yield return new TestFixtureData("string value2a", "string value2b");
            yield return new TestFixtureData("string value3a", "string value3b");
        }
    }
}

问题是OneTimeSetUpOneTimeTearDown方法只运行一次,但是我希望它们能够为每个fixture实例运行(在我的情况下是三个实例)。

我错过了什么吗?

0 个答案:

没有答案