如何将Nunit与不同的类和TextFixtureSetup和TearDown一起使用

如何将Nunit与不同的类和TextFixtureSetup和TearDown一起使用,nunit,Nunit,我有一个大问题,我有3个测试类,但我创建了其他类来在数据库中插入假数据来测试我的类,但我创建了其他类来删除我创建的数据 但是我在类中使用了[SetUp]来创建假数据,在类中使用了[TearDown]来删除数据 但是使用[SetUp]或[TestFixtureSetUp]创建了两次数据并进行了一次测试,但是当我完成类时,自动类使用teardown或TextFixtureTearDown完成,并且不启动其他测试另一个测试发生在teardown 在运行所有测试装置之前,是否可以编写一个类来用测试数据填

我有一个大问题,我有3个测试类,但我创建了其他类来在数据库中插入假数据来测试我的类,但我创建了其他类来删除我创建的数据

但是我在类中使用了[SetUp]来创建假数据,在类中使用了[TearDown]来删除数据

但是使用[SetUp][TestFixtureSetUp]创建了两次数据并进行了一次测试,但是当我完成类时,自动类使用teardownTextFixtureTearDown完成,并且不启动其他测试另一个测试发生在teardown


在运行所有测试装置之前,是否可以编写一个类来用测试数据填充数据库,然后在所有测试类运行之后让它删除测试数据?

如果我理解您的问题,我认为您可以使用公共基类进行测试:

public class TestBase{
  [SetUp]
  public void BaseSetUp(){
     // Set up all the data you need for each test
  }

  [Teardown]
  public void BaseTeardown(){
     // clean up all the data for each test
  }
}

[TestFixture]
public class TestClass : TestBase{
  [SetUp]
  public void LocalSetUp(){
     // Set up all the data you need specifically for this class
  }

  [Teardown]
  public void LocalTeardown(){
     // clean up all specific data for this class
  }

  [Test]
  public void MyTest(){
        // Test something
  }

}
这样,您的所有设置和拆卸都可以共享,并在每次测试之前运行。您可以验证这一点(我是从内存中执行的),但我相信运行顺序是:

  • TestBase.BaseSetup()
  • TestClass.LocalSetup()
  • TestClass.MyTest()
  • TestClass.LocalTeardown()
  • TestBase.BaseTeardown()
编辑: 好的,现在我更好地理解了您的要求,我认为您可以使用该属性来确保您的数据设置和拆卸只在整个测试套件中发生一次

因此,您可以按如下方式设置单独的设置类,而不是公共基类:

   [SetupFixture]
   public class TestSetup{
      [SetUp]
      public void CommonSetUp(){
         // Set up all the data you need for each test
      }

      [TearDown]
      public void CommonTeardown(){
         // clean up all the data for each test
      }
    }

    [TestFixture]
    public class TestClass1 {
      [SetUp]
      public void LocalSetUp(){
         // Set up all the data you need specifically for this class
      }

      [Teardown]
      public void LocalTeardown(){
         // clean up all specific data for this class
      }

      [Test]
      public void MyTest(){
            // Test something
      }

    [TestFixture]
    public class TestClass2 {
      [SetUp]
      public void LocalSetUp(){
         // Set up all the data you need specifically for this class
      }

      [Teardown]
      public void LocalTeardown(){
         // clean up all specific data for this class
      }

      [Test]
      public void MyTest(){
            // Test something
      }

    }
然后,操作顺序将类似于:

  • TestSetup.CommonSetup()
  • TestClass1.LocalSetup()
  • TestClass1.MyTest()
  • TestClass1.LocalTeardown()
  • TestClass2.LocalSetup()
  • TestClass2.MyTest()
  • TestClass2.LocalTeardown()
  • TestSetup.commondeardown()

注意:您的所有测试必须在同一命名空间中。

几乎就是这个Mike,我想在数据库中插入数据一次,然后开始执行所有测试,当我完成所有测试时,我将删除数据库中的数据。但这个表单在每个测试中设置、拆卸和继承,在数据库中插入数据,并在每个测试中删除数据。你现在知道怎么做了吗?在你的基类中,使用FixtureSetup和FixtureTeardown属性,而不是我之前建议的。我做了这个,但是每个测试类,它都会再次调用FixtureSetup并完成类调用teardown base。我想为所有类创建一次,为所有类创建一次拆卸,而不是为每个类创建一个。我已经为我的答案添加了另一个解决方案-请参见上面的编辑。太棒了!请将答案标记为已接受,以便将来的用户知道这是一个好答案。