MbUnit now supports the new attributes for Roy Osherove (ISerializable) to solve the database Rollback problem:
- SqlRestoreInfoAttribute contains the information necessary to perform database restore (connection string, etc...),
- RollBackAttribute uses EnterpriseServices to roll back the transactions done in the test case, (Note that this attribute does not rely on SqlRestoreInfo and can live on its own)
- RestoreDatabaseFirstAttribute, restores the database before starting the test (using DbAdministrator from TestFu).
I will assume that you have read the article from Roy so I can skip explanation and show an example. Consider the following test fixture:
[TestFixture]
[SqlRestoreInfo("connectionstring","databasename",@"c:\backups\nw.mbk")]
public class NorthWindTest
{
[Test, RollBack]
public void TestWithRollBack()
{...}
[Test, RestoreDatabaseFirst]
public void TestWithRestoreFirst()
{...}
}
This example, which runs in MbUnit, is similar to what Roy has proposed: SqlRestoreInfo gives information that can be used to restore the db. TestWithRollBack is rolled back using Enterprise services, the database is restored before TestWithRestoreFirst is executed.
What about data abstraction ?
We would like to create a fixture and apply it to different Db provider (Oracle, MySql,etc..). Is this possible ? This is (will**) possible in a minimum of work, it is just a matter of changing SqlRestoreInfo to OracleRestoreInfo:
[TestFixture]
public abstract class DbNorthwindTest
{
[Test, RollBack]
public void TestWithRollBack()
{...}
[Test, RestoreDatabaseFirst]
public void TestWithRestoreFirst()
{...}
}
[SqlRestoreInfo("connectionstring","databasename",@"c:\backups\nw.mbk")]
public class SqlNorthwindTest : DbNorthwindTest
{}
[OracleRestoreInfo("connectionstring","databasename",@"c:\backups\nwporacle.mbk")]
public class OracleNorthwindTest : DbNorthwindTest
{}
This example is quite neat and self-explenatory: SqlNorthwindTest will apply the fixture against a MsSql server using System.Data.SqlClient classes, while OracleNorthwindTest will test against Oracle.
**Currently, only SqlRestoreInfoAttribute is implemented.