如果我理解您的问题,我认为您可以使用通用基类进行测试:
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()
- 测试类.MyTest()
- TestClass.LocalTeardown()
- TestBase.BaseTeardown()
编辑:
好的,既然我更好地理解了您的要求,我认为您可以使用SetupFixture属性来确保您的数据设置和拆卸只为您的完整测试套件发生一次。
因此,您将设置一个单独的设置类,而不是通用基类,如下所示:
[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.CommonTeardown()
注意:您的所有测试都必须在同一个命名空间中。