Ian Boyd
Ian Boyd

Reputation: 256999

How to integration test an object with database queries

How can i write unitintegration tests that talk to a database. e.g.:

public int GetAppLockCount(DbConnection connection)
{
    string query := 
          "SELECT"+CRLF+
          "   tl.resource_type AS ResourceType,"+CRLF+
          "   tl.resource_description AS ResourceName,"+CRLF+
          "   tl.request_session_id AS spid"+CRLF+
          "FROM sys.dm_tran_locks tl"+CRLF+
          "WHERE tl.resource_type = 'APPLICATION'"+CRLF+
          "AND tl.resource_database_id = ("+CRLF+
          "    SELECT dbid"+CRLF+
          "    FROM master.dbo.sysprocesses"+CRLF+
          "    WHERE spid = @@spid)";

    IRecordset rdr = Connection.Execute(query);

    int nCount = 0;
    while not rdr.EOF do
    {
       nCount := nCount+1;
       rdr.Next;
    }

    return nCount;
 }

In this case i am trying to exorcise this code of bugs (the IRecordset returns empty recordset).

[UnitTest]
void TestGetLockCountShouldAlwaysSucceed();
{
   DbConnection conn = GetConnectionForUnit_IMean_IntegrationTest();
   GetAppLockCount(conn);
   CheckTrue(True, "We should reach here, whether there are app locks or not");
}

Now all i need is a way to connect to some database when running a unit integration testing.

Do people store connection strings somewhere for the test-runner to find? A .ini or .xml or .config file?


Note: Language/framework agnostic. The code intentionally contains elements from:

in order to drive that point home.

Upvotes: 0

Views: 263

Answers (1)

Michael Lloyd Lee mlk
Michael Lloyd Lee mlk

Reputation: 14661

Now all i need is a way to connect to some database when running a unit integration testing.

Either use an existing database or an in-memory database. I've tried both an currently use an existing database that is splatted and rebuilt using Liquibase scripts in an ant file. Advantages to in-memory - no dependencies on other applications. Disadvantages - Not quite as real, can take time to start up. Advantages to real database - Can be identical to the real world Disadvantages - Requires access to a 3rd party machine. More work setting up a new user (i.e. create new database)

Do people store connection strings somewhere for the test-runner to find? A .ini or .xml or .config file?

Yeap. In C# I used a .config file, in java a .props file. With in-memory you can throw this into the version control as it will be the same for each person, with a real database running somewhere it will need to be different for each user.

You will also need to consider seed data. In Java I've used dbUnit in the past. Not the most readable, but works. Now I use a Ruby ActiveRecord task.

How do you start this? First can you rebuild your database? You need to be able to automate this before going to far down this road.

Next you should build up a blank local database for your tests. I go with one-per-developer, some other teams share but don't commit. In a .NET/MS SQL world I think in memory would be quite easy to do.

Upvotes: 1

Related Questions