Tuesday, July 20, 2004

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.

posted on Wednesday, July 21, 2004 3:51:00 AM UTC  #    Comments [22]
Tracked by:
"play free poker online" (play free poker online) [Trackback]
"ie privacy keeper" (online) [Trackback]
Monday, June 06, 2005 5:48:00 PM UTC
Wow, nice!
<br>
<br>I don't get how you can be so fast at putting these things together.
Steve Willer
Monday, June 06, 2005 5:48:00 PM UTC
Peli's Blog
Monday, June 06, 2005 5:48:00 PM UTC
You should mention that the Rollback attribute does not rely on the sqlRestore attribute and can be run on its on. the sqlInfoRestore and RestoreDatabaseFirst are the ones who should live side by side.
Roy Osherove
Monday, June 06, 2005 5:48:01 PM UTC
ISerializable
Monday, June 06, 2005 5:48:01 PM UTC
Peli's Blog
Monday, June 06, 2005 5:48:02 PM UTC
ISerializable
Monday, June 06, 2005 5:48:02 PM UTC
Peli's Blog
Monday, June 06, 2005 5:48:03 PM UTC
Peli's Blog
Monday, June 06, 2005 5:48:03 PM UTC
Aaron Junod
Monday, June 06, 2005 5:48:03 PM UTC
Just to be clear for us simple-minded...you still need to set up the stuff that Roy talks about such as the strong naming and the use of the System.EnterpriseServices. You're implementation is just to make allow these.
Mike
Monday, June 06, 2005 5:48:04 PM UTC
Never Mind... I got the rollback to work in the GUI - but not under <a title="" href="http://weblogs.asp.net/nunitaddin/" target="_blank">NUnitAddIn</a>
Mike
Monday, June 06, 2005 5:48:04 PM UTC
I have a meeting with Jamie Cansdale tomorow to work on <a title="MbUnit, Generating Unit Testing and Model Based Testing Framework for .NET Framework" href="http://mbunit.tigris.org" target="_blank">MbUnit</a>-NUnitAddin integration, we'll look at that.
Jonathan de Halleux
Monday, June 06, 2005 5:48:04 PM UTC
Peli's Blog
Monday, June 06, 2005 5:48:05 PM UTC
Peli's Blog
Monday, June 06, 2005 5:48:05 PM UTC
dotRob
Monday, June 06, 2005 5:48:06 PM UTC
Do u have any idea how to solve this problem while trying to restore nw database ?
<br>
<br> Exclusive access could not be obtained because the database is in use.
<br>RESTORE DATABASE is terminating abnormally.
<br> System.Data.SqlClient.SqlException
<br> Message: Exclusive access could not be obtained because the database is in use.
<br> RESTORE DATABASE is terminating
<br>abnormally.
Dimitar Kolev
Monday, June 06, 2005 5:48:06 PM UTC
It seems that all the connection opened onthe database are not closed. Remember that even if you invoke SqlConnection close, the server keeps a pool of connecton alive.
<br>
<br>In fact, you could do a 'tasklist -m sqlserver.exe' to see which process are using the server. Are you using SqlAdministrator for this ?
Jonathan de Halleux
Monday, June 06, 2005 5:48:06 PM UTC
I tried to restart SQL server in order to close all opend connections but i have the same problem i have this problem when i try to make the test defined in this article i am trying only this. Do u have any idea how to cose all SQL connections before trying to execute the test?
Dimitar Kolev
Monday, June 06, 2005 5:48:07 PM UTC
Have you tried stopping the SQLSERVERAGENT
dazzle
Monday, June 06, 2005 5:48:07 PM UTC
that should do it, i had the same issue and stopping the sqlserveragent allowed me to restore msdb db
<br>
<br>thanks for the tuip, dazzle
just me
Monday, June 06, 2005 5:48:07 PM UTC
that should do it, i had the same issue and stopping the sqlserveragent allowed me to restore msdb db
<br>
<br>thanks for the tip, dazzle
just me
Monday, June 06, 2005 5:48:08 PM UTC
Wow, superb!
<br>Thanks.
Diana
Comments are closed.