Configuring your DBTestUnit test project

There are two places that need to be configured to allow tests to run:

1. The test dll config file.
2. The ‘dbInstance’ property set in the test classes.

This blog will explain how to config each of these.

Test dll config file
If you have downloaded DBTestUnit and followed the instructions outlined here – you will find the test dll config file – AdventureWorks.DatabaseTest.dll.config – in the following dir:

C:\Projects\AdventureWorksBlog\src\AdventureWorksDatabaseTest\bin\Debug\

This contains the connections settings for the database(s) that will be tested.

The following text shows a sample database connection for a MS SQL server from this file.

<connectionStrings>
<add name="AdventureWorks" connectionString="Data Source=serverName;Initial Catalog=AdventureWorks;Integrated Security=True;Application Name=AdventureWorksUnitTesting" providerName="System.Data.SqlClient"/>
</connectionStrings>

There are two parts to configuring a MS SQL server database

A) The element ‘name’ shown above as – name=”AdventureWorks”. This is referenced within the test classes.
B) The element ‘connectionString’. This is a standard SQL server connection string – consisting of:

  • ‘Data Source=serverName’ – the SQL server to connect to.
  • ‘Initial Catalog=AdventureWorks’ – the database to test.
  • ‘Integrated Security=True’ – use trusted security ie uses the account that the tests are being run under.

Note DBTestUnit requires read access to information_schema and system views. Therefore, the account being used will need appropriate access.

Test class
The sample code, shown below, is for testing a sproc in the AdventureWorks db.

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

namespace AdventureWorks.DatabaseTest.Tests.Sprocs.Schema
{
    [TestFixture]
    public class uspGetManagerEmployees : SprocTestBase
    {
        public uspGetManagerEmployees()
        {
            dbInstance = "AdventureWorks";

As can be seen the ‘dbInstance’ property is set to ‘AdventureWorks’. This test class would use the connection settings in the config file set by the element ‘name’ which has a value of ‘AdventureWorks’.

Example
Imagine the scenario where you want to carry out both unit and deployment tests using the same Visual Studio project ie only having one test dll and config file. But, you want to use different SQL servers/databases for each type of test – with settings as outlined below:

Type SQL server Database dbInstance
Unit test SQLServer01 TestDb MyUnitTests
Deployment test SQLServer02 DeploymentDb MyDeploymentTests

To do this the following would be added to the test dll config file.

<connectionStrings>
<add name="MyUnitTests" connectionString="Data Source=SQLServer01;Initial Catalog=TestDb;Integrated Security=True;Application Name=UnitTesting" providerName="System.Data.SqlClient"/>

<add name="MyDeploymentTests" connectionString="Data Source=SQLServer02;Initial Catalog=DeploymentDb;Integrated Security=True;Application Name=DeploymentTesting" providerName="System.Data.SqlClient"/>
</connectionStrings>

Each type of test class would also need to reference the appropriate configuration settings – ‘dbInstance’ would be set as follows in each type of test class.

Unit test

    [TestFixture]
    public class someUnitTest
    {
        public someUnitTest()
        {
            dbInstance = "MyUnitTests";

Deployment test

    [TestFixture]
    public class someDeploymentTest
    {
        public someDeploymentTest()
        {
            dbInstance = "MyDeploymentTests";

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: