Initial set up and configuration of DBTestUnit

Last modified: 2011-06-08

This is the first in a two part series on how to set up the Visual Studio (VS) template – that is included in the DBTestUnit download.

The second will show how to run tests.

If you already have an existing solution/project and want to start using DBTestUnit – a section is included at the end to cover this scenario.

The following short screencast can be used with the notes below to help set up and configure.

(Screencast was created in Aug 2010)

1. Download the latest version of the DBTestUnit

2. Extract files to where you want to place your database testing solution/project.

In the example below it has been extracted to C:\Projects\ :

DBTemplate default directory structure

DBTemplate default directory structure

3. Rename dirs and files with your database name.

As you can see in the image above by default ‘DBTemplate’ is used as a prefix for many dirs and files.

These should be replaced with the name of the database to be tested.

For this I am going to use the MS sample database AdventureWorks as an example.

Therefore change:

C:\Projects\DBTemplate\ to

Rather than renaming these manually, a bat file – C:\Project\DBTemplate\tools\DBTemplateSetUp.bat – is provided that can do this.

The first section of this file is shown below.

SET dirRoot=C:\Projects\
SET dirParent=%dirRoot%DBTemplate\
SET dbProjectName=AdventureWorks

Before running the bat file – copy it to another directory eg C:\Projects\ and set appropriate values for ‘dirRoot’ and ‘dbProjectName’.

After renaming the directory structure should be similar to the following:

AdventureWorks directory structure

AdventureWorks directory structure

4. Change the VS solution file

Open – C:\Project\AdventureWorks\src\AdventureWorks.sln

Replace ‘DBTemplate’ with ‘AdventureWorks’

5. Change the the db test project file

Open – C:\Projects\AdventureWorks\src\AdventureWorksDatabaseTest\

Replace ‘DBTemplate’ with ‘AdventureWorks’

6. Start VS

Open the solution.

The structure should look similar to the following:

AdventureWorks Visual Studio directory structure

AdventureWorks Visual Studio directory structure

A number of sample unit tests and other files are including the in solution. These will all have the text ‘DBTemplate’ in their namespaces.

Therefore, do a solution wide ‘find and replace’ changing ‘DBTemplate’ to ‘AdventureWorks’

7. Set up the config file to connect to the database to test

If you open up any of the sample tests provided you will see that they all have a variable – dbInstance – which by default has the value ‘AdventureWorks’.

This can be seen at the bottom of the sample code provided below:

using System;
using MbUnit.Framework;
using DBTestUnit.InfoSchema;
using DBTestUnit.UnitTestBaseClass.MSSQL;

namespace DBTemplate.DatabaseTest.Tests.Tables.Schema
    public class SalesOrderHeader : TableTestBase
        public SalesOrderHeader()
            dbInstance = "AdventureWorks";

This is used to provide connection settings to the database to be tested and should be set in the following app config file:


Open this config file.

Go to the connectionStrings section.

The following should be included by default.

<!--MS SQL dbInstances-->
<add name="AdventureWorks" connectionString="Data Source=serverName;Initial Catalog=AdventureWorks;Integrated Security=True;Application Name=AdventureWorksUnitTesting" 

The ‘name=”AdventureWorks”‘ should correlate with the value set in the variable ‘dbInstance’ as show in the test code above.

The connectionString properties need to be changed appropriately eg Data Source=serverName

Once the config file has been updated – that’s it all set up – ready to start database testing.

What do I do if I have an existing project?
If you an existing project and just want to start using the DBTestUnit.dll for testing then it is a bit easier to set up.

Download the latest version of the DBTestUnit as per step 1.

In your existing test project reference the latest version of DBTestUnit.dll. You will also need to reference the MS enterprise libraries provided in the download.

Then amend your existing config file appropriately so that tests can connect to the database.

Round up
This blog shown how to set up DBTestUnit – the second will look at running tests.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: