Nunit Base Text Fixtures及其运行顺序

时间:2012-04-18 15:33:52

标签: c# unit-testing nunit

如果我有:

[TestFixture]
public class BaseTestFixture
{
  [TestFixtureSetup]
  public void SetUpStuff()
  {

  }
}

[TestFixture]
public class DeriveTestFixture : BaseTextFixture
{
   [TestFixtureSetup]
   public void SetupOtherStuff()
   {
   } 
}

在DerivedTestFixture TestFixtureSetUp方法之前或之后是否调用BaseTextFixture TestFixtureSetup方法?

1 个答案:

答案 0 :(得分:1)

为什么不通过测试向自己证明?

    [TestFixture]
    public class BaseTestFixture
    {
      [TestFixtureSetup]
      public void SetUpStuff()
      {
         Console.Writeline("Base");
      }
    }

    [TestFixture]
    public class DeriveTestFixture : BaseTextFixture
    {
       [TestFixtureSetup]
       public void SetupOtherStuff()
       {
         Console.Writeline("Derived");
       } 
    }

那说你可能会考虑只在基础上拥有属性并且有两个其他函数来覆盖,比如OnAfterTestFixtureSetup(),所以它更明确。也就是说,

[TestFixture]
    public class BaseTextFixture
    {
      [TestFixtureSetup]
      public void SetUpStuff()
      {
         Console.Writeline("Base");
        OnAfterTextFixtureSetup();
      }

      public virtual OnAfterTextFixtureSetup()
      {

      }
}