Create Microsoft sql database before integration test

16 Ott

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))
      using (IDbCommand command = connection.CreateCommand())
            command.CommandType = CommandType.Text;
            command.CommandText = "CREATE DATABASE UnitTestDemo";
//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 Risposte to “Create Microsoft sql database before integration test”

  1. Alexander Byndyu ottobre 17, 2008 a 5:49 am #

    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?

  2. makka ottobre 17, 2008 a 7:16 am #

    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.

  3. Alexander Byndyu ottobre 17, 2008 a 8:44 am #

    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.


Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )


Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: