简体   繁体   English

带参数的Nunit测试设置方法

[英]Nunit test setup method with argument

Can we have a test set up method with arguments? 我们可以有一个带有参数的测试设置方法吗? I need a different set up for every test in a fixture. 我需要为夹具中的每个测试设置不同的设置。

Do we have something (or similar way) as the hypothetical idea : 我们是否有某种(或类似的方式)假设的想法:

[SetUp]
[Argument("value-1")]
[Argument("value-2")]
[Argument("value-3")]
public void InitializeTest(string value)
{
    //set env var with value
}

It can be done using the TestFixture attribute with a parameter. 可以使用带有参数的TestFixture属性来完成。

If all the tests in the class depend on the same parameter this is the way to go. 如果该类中的所有测试都依赖于相同的参数,这就是方法。

The class will need a constructor with the same parameter passed to the TestFixture attribute. 该类将需要一个构造函数,该构造函数具有传递给TestFixture属性的相同参数。

See Parameterized Test Fixtures on https://github.com/nunit/docs/wiki/TestFixture-Attribute 请参阅https://github.com/nunit/docs/wiki/TestFixture-Attribute上的参数化测试治具

[TestFixture("Oscar")]
[TestFixture("Paul")]
[TestFixture("Peter")]
public class NameTest
{
    private string _name;

    public NameTest(string name)
    {
        _name = name;
    }

    [Test]
    public void Test_something_that_depends_on_name()
    {
        //Todo...
    }

    [Test]
    public void Test_something_that_also_depends_on_name()
    {
        //Todo...
    }
    //...
}

This code is from the nunit documentation website: 此代码来自nunit文档网站:

[TestFixture("hello", "hello", "goodbye")]
[TestFixture("zip", "zip")]
[TestFixture(42, 42, 99)]
public class ParameterizedTestFixture
{
    private readonly string eq1;
    private readonly string eq2;
    private readonly string neq;

    public ParameterizedTestFixture(string eq1, string eq2, string neq)
    {
        this.eq1 = eq1;
        this.eq2 = eq2;
        this.neq = neq;
    }

    public ParameterizedTestFixture(string eq1, string eq2)
        : this(eq1, eq2, null)
    {
    }

    public ParameterizedTestFixture(int eq1, int eq2, int neq)
    {
        this.eq1 = eq1.ToString();
        this.eq2 = eq2.ToString();
        this.neq = neq.ToString();
    }

    [Test]
    public void TestEquality()
    {
        Assert.AreEqual(eq1, eq2);
        if (eq1 != null && eq2 != null)
            Assert.AreEqual(eq1.GetHashCode(), eq2.GetHashCode());
    }

    [Test]
    public void TestInequality()
    {
        Assert.AreNotEqual(eq1, neq);
        if (eq1 != null && neq != null)
            Assert.AreNotEqual(eq1.GetHashCode(), neq.GetHashCode());
    }
}

Setup is executed once per each tests and for one tests there is only one SetUp and TearDown. 设置在每个测试中执行一次,对于一个测试,只有一个SetUp和TearDown。 You can call your Initialize method from tests explicitly and then create Data-Driven tests using TestCase attribute 您可以从测试中显式调用Initialize方法,然后使用TestCase属性创建数据驱动的测试

public void InitializeTest(string value)
{
    //set env var with value
}

[TestCase("Value-1")]
[TestCase("Value-2")]
[TestCase("Value-3")]
public void Test(string value)
{
    InitializeTest(value);

    //Arange
    //Act
    //Assert
}

As result, you will have three tests each calling InitializeTest with different parameters 结果,您将拥有三个测试,每个测试使用不同的参数调用InitializeTest

设置方法用于执行一些preTest作业,其中包括准备测试,例如设置运行测试所需的任何值,您可以在设置方法中设置这些值,而不必提供这些值作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM