AssemblyCleanup() after test fail/exception

Posted by Tommy Jakobsen on Stack Overflow See other posts from Stack Overflow or by Tommy Jakobsen
Published on 2010-05-09T14:13:43Z Indexed on 2010/05/09 14:18 UTC
Read the original article Hit count: 1110

Filed under:
|
|

Hello,

I'm running a few unit tests that requires a connection to the database. When my test project get initialized, a snapshot of the database is created, and when tests are done the database gets restored back to the snapshot.

Here is the implementation:

[TestClass]
public static class AssemblyInitializer
{
    [AssemblyInitialize()]
    public static void AssemblyInit(TestContext context)
    {
        var dbss = new DatabaseSnapshot(...);    
        dbss.CreateSnapshot();
    }

    [AssemblyCleanup()]
    public static void AssemblyCleanup()
    {
        var dbss = new DatabaseSnapshot(...);
        dbss.RevertDatabase();
    }
}

Now this all works, but my problem arise when I have a failing test or some exception. The AssemblyCleanup is of course not invoked, so how can I solve this problem? No matter what happens, the snapshot has to be restored. Is this possible?

© Stack Overflow or respective owner

Related posts about c#

Related posts about unit-testing