Multiple ClassCleanup Attributes for Each Test Class

The situation . Some of my integration test classes use a common approach to setting up scripts in the database, so I provide an abstract base class. He also takes care to completely clear the data at the end after all the tests:

public abstract class IntegrationTests
{
    ...
    protected static void Cleanup() { ... }
}

My legacy classes should call the base method to make sure my database cleanup code works:

[TestClass]
public class FooIntegrationTests : IntegrationTests
{
    ...

    [ClassCleanup]
    public static void FooCleanup()
    {
        ...
        Cleanup();
    }
}

Problem . According to MSDN, "[one] one method in a class can be decorated with [ClassCleanup]", so I cannot decorate a method Cleanupfrom a base class, and even if I did, the method would not receive a call.

Question : I need a solution that

  • Cleanup -
  • , .

, , . , (!) . ?

. , , , , .

+4
1

, ClassCleanup .

. , , "" . / , , , :

[TestClass]
public abstract class BaseIntegrationTest
{
    [TestInitialize]
    public void BeforeEach() {
        // Stuff that should happen before each unit test

        BaseTestInitialize();
    }

    [TestCleanup]
    public void AfterEach(){
        // Stuff that should happen after each unit test

        BaseTestCleanup();
    }

    public virtual void BaseTestInitialize() { }
    public virtual void BaseTestCleanup() { }
}

, , - BaseTestCleanup() :

[TestClass]
public class DerivedTestClass : BaseIngetrationTest
{
    public override void BaseTestCleanup()
    {
        // Derived cleanup
        base.BaseTestCleanup();
    }


    [TestMethod]
    public void SomeMethod_SomeCriteria_SomeResult()
    {
        // Arrange

        // Act

        // Assert
    }
}
0

All Articles