Database testing with Mono and NUnit

In these days I’m trying to rewrite an old application using Mono.
I’m currently a newbie about Mono and the .Net world, but I happily noticed that the Mono environment come with a bundled interface to NUnit, so I can keep on writing tests as I’m slowly but steadily getting used to.
After having put together a bunch of classes and relative and unit tests, it was time to write the first integration test. I needed to put the development database in a known state before running the test, so I came up with the following class, that I’d like to share with you.

  1. using System;
  2. using System.Collections;
  3. using NUnit.Framework;
  4. using System.IO;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. namespace TellItMailer{
  8.  public class TestDbSetup{
  9.   public void SetUpDbForTest(object test){
  10.    String path = test.GetType().FullName + ".sql";
  11.    path = path.Replace(this.GetType().Namespace + ".", "");
  12.    path = "/home/danidemi/workspace/TellItMailer/Tests/" + path;
  13.    //Read the script
  14.    String script = "";
  15.    using (StreamReader sr = File.OpenText(path)){
  16.     string s = "";
  17.                while ((s = sr.ReadLine()) != null){
  18.      script = script + s;
  19.                }
  20.           }
  21.  
  22.    string connectionString =
  23.     "Server=192.168.75.128\\SQLEXPRESS;" +
  24.     "Database=TellIt_test;" +
  25.              "User ID=user;" +
  26.              "Password=password;";
  27.  
  28.           IDbConnection dbcon;
  29.    using(dbcon = new SqlConnection(connectionString)){
  30.     dbcon.Open();
  31.            using (IDbCommand dbcmd = dbcon.CreateCommand()) {
  32.                dbcmd.CommandText = script;
  33.      dbcmd.ExecuteNonQuery();
  34.     }
  35.    }
  36.   }
  37.  
  38.  }
  39. }

It’s quite useful. Essentially this class loads a SQL script file whose name is the same of the test class, and execute it.
In this way you have for example a TestFeature.cs file alongside a TestFeature.sql file. Nice if your IDE shows files sorted alphabetically.
I found that the best place to use the class is inside the SetUp marked method. In this way the SQL script is run before each test. Doing it in a local development environment is not so bad, since the scripts are executed at high speed.

So, this is an example of how a test that uses TestDbSetup looks like.

  1. using System;
  2. using NUnit.Framework;
  3. using log4net;
  4. using log4net.Config;
  5. namespace TellItMailer {
  6.  [TestFixture()]
  7.  public class IntegrationTest {
  8.   [SetUp]
  9.   public void SetUp(){
  10.    new TestDbSetup().SetUpDbForTest(this);
  11.   }
  12.  
  13.   [Test]
  14.   public void TestCase(){
  15.    BasicConfigurator.Configure();
  16.   }
  17.  }
  18. }

Pretty useful, IMHO.