简体   繁体   English

基类中的NUnit和[SetUp]

[英]NUnit and [SetUp] in base classes

I'm looking at some test code using NUnit, which inherits from a base class containing a [SetUp] attribute: 我正在使用NUnit查看一些测试代码,NUnit继承自包含[SetUp]属性的基类:

public class BaseClass
{
   [SetUp]
   public void SetUp()
   {
     //do something
   }

}

[TestFixture]
public class DerivedClass : BaseClass
{
  [SetUp]
  public void SetUp()
  {

   //do something else, with no call to base.SetUp()
  }
   //tests run down here.
   //[Test]
   //[Test]
   //etc
}

The derived class will certainly need the work done in the base class' SetUp() method. 派生类肯定需要在基类'SetUp()方法中完成的工作。

Am I missing something, or will the SetUp() method in the base class not be called when the derived class's tests are run? 我是否遗漏了某些内容,或者在运行派生类的测试时是否会调用基类中的SetUp()方法? Is there something special with the [SetUp] attribute that ensures one will be called before the other? [SetUp]属性是否有特殊功能可以确保在另一个之前调用一个?

Before NUnit 2.5 the previous answers were correct; 在NUnit 2.5之前,之前的答案是正确的; you could only have a single [SetUp] attribute for a test. 您只能为测试设置一个[SetUp]属性。

With NUnit 2.5 onwards you can have multiple methods decorated with the [SetUp] attribute. 使用NUnit 2.5以后,您可以使用[SetUp]属性修饰多个方法。 Therefore the below is perfectly valid in NUnit 2.5+: 因此,以下在NUnit 2.5+中完全有效:

public abstract class BaseClass
{
    [SetUp]
    public void BaseSetUp()
    {
        Debug.WriteLine("BaseSetUp Called")
    }
}

[TestFixture]
public class DerivedClass : BaseClass
{
    [SetUp]
    public void DerivedSetup()
    {
        Debug.WriteLine("DerivedSetup Called")  
    }

    [Test]
    public void SampleTest()
    {
        /* Will output
         *    BaseSetUp Called
         *    DerivedSetup Called
        */
    }
}

When inheriting NUnit will always run the '[SetUp]' method in the base class first. 继承NUnit时,首先会在基类中运行'[SetUp]'方法。 If multiple [SetUp] methods are declared in a single class NUnit cannot guarantee the order of execution. 如果在单个类中声明了多个[SetUp]方法,则NUnit无法保证执行的顺序。

See here for further information . 请参阅此处获取更多信息

You can only have one SetUp method. 您只能有一个SetUp方法。

A TestFixture can have only one SetUp method. TestFixture只能有一个SetUp方法。 If more than one is defined the TestFixture will compile successfully, but its tests will not run. 如果定义了多个,则TestFixture将成功编译,但其测试将不会运行。

http://www.nunit.org/index.php?p=setup&r=2.2.10 http://www.nunit.org/index.php?p=setup&r=2.2.10

If you need to add additional setup logic in a child class, mark SetUp as virtual in your parent class, override it, and call base.SetUp() if you want the base class's setup to run, too. 如果需要在子类中添加其他设置逻辑,请在父类中将SetUp标记为虚拟,覆盖它,如果希望基类的设置也运行,则调用base.SetUp()

public class BaseClass
{
   [SetUp]
   public virtual void SetUp()
   {
     //do something
   }

}



[TestFixture]
public class DerivedClass : BaseClass
{
  public override void SetUp()
  {
   base.SetUp(); //Call this when you want the parent class's SetUp to run, or omit it all together if you don't want it.
   //do something else, with no call to base.SetUp()
  }
   //tests run down here.
   //[Test]
   //[Test]
   //etc
}

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

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