有没有办法用参数创建nunit安装程序

时间:2013-02-22 17:18:28

标签: c# nunit

有没有办法为这样的nunit设置方法添加参数:public void SetUp(Point p = null) { /*code*/ }

我尝试了它并获得了以下异常SetUp : System.Reflection.TargetParameterCountException : Parameter count mismatch

1 个答案:

答案 0 :(得分:1)

我认为您的观点是避免代码重复。 尝试使用SetUp()中使用的overriten方法提取基类。 所有派生类都将从基类执行测试,其中的对象是在OverritUp()

中编写的
[TestFixture]
public class BaseTestsClass
{
    //some public/protected fields to be set in SetUp and OnSetUp

    [SetUp]
    public void SetUp()
    {
        //basic SetUp method
        OnSetUp();
    }

    public virtual void OnSetUp()
    {
    }

    [Test]
    public void SomeTestCase()
    {
        //...
    }

    [Test]
    public void SomeOtherTestCase()
    {
        //...
    }
}

[TestFixture]
public class TestClassWithSpecificSetUp : BaseTestsClass
{
    public virtual void OnSetUp()
    {
        //setup some fields
    }
}

[TestFixture]
public class OtherTestClassWithSpecificSetUp : BaseTestsClass
{
    public virtual void OnSetUp()
    {
        //setup some fields
    }
}

使用参数化的TestFixture也很有用。课堂上的测试将按照TestFixture,SetUp方法进行。 但请记住

  

参数化装置(正如您所发现的那样)受限于您只能使用属性中允许的参数

用法:

[TestFixture("some param", 123)]
[TestFixture("another param", 456)]
public class SomeTestsClass
{
    private readonly string _firstParam;
    private readonly int _secondParam;

    public WhenNoFunctionCodeExpected(string firstParam, int secondParam)
    {
        _firstParam = firstParam;
        _secondParam = secondParam;
    }

    [Test]
    public void SomeTestCase()
    {
        ...
    }
}
相关问题