TDD developer

October 16, 2008

Create Microsoft sql database before integration test

Filed under: Uncategorized — makka @ 7:56 pm
Tags: , , ,

In my last post I shown how to create a SqlCe database “on the fly” once before all integration tests. Alexander asked me how can do the same thing but using a Microsoft Sql database. Let’ s how we can do it:

using (IDbConnection connection = new SqlCeConnection(masterConnectionString))
{
    connection.Open();
      using (IDbCommand command = connection.CreateCommand())
        {
            command.CommandType = CommandType.Text;
            command.CommandText = "CREATE DATABASE UnitTestDemo";
            command.ExecuteNonQuery();
        }
    }
}
//open a new connection to UnitTestDemo database and create all objects
The only difference here is the statement used for the creation of the database. As you can see we need a connection to the master database in order to execute the CREATE DATABASE statement. Once we have the new empty database we can close this first connection, open a new one to test database and run all the scripts for the objects (tables, views, stored procedures, ecc.) creation.
 

3 Comments »

  1. Thanks for answer! =)

    We will try to use it on next project.

    I think we always should have ‘installation’ script for MOCK DB and script for insert MOCK data into mock DB.

    How would you keep this scripts actual?

    Comment by Alexander Byndyu — October 17, 2008 @ 5:49 am | Reply

  2. Very simple!
    Our solutions are all “continuos integrated” that means after each code commit all test databases are destroied and that recreated using scripts.
    If one of these scripts is out-of-date the build is broken!

    We don’t use Managment Studio to edit database schema.
    We learned (believe me, it’s not difficult!) the scripts needed to create and alter objects in database.
    Following this approach we have completed source T-SQL code (always tested) for database creation that is very usefull during deployment.

    Comment by makka — October 17, 2008 @ 7:16 am | Reply

  3. Clear…
    We are using CI (CC.NET) but only with in-memory tests.
    I think we can add night build event with DB interaction tests to check our integration tests on MOCK DB.

    Comment by Alexander Byndyu — October 17, 2008 @ 8:44 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.