TechBubbles Microsoft Technology BLOG

Creating a Database Unit Test using Visual Studio 2010

 

This post discusses about creating a database unit test case in Visual Studio 2010 which verifies the functionality of a stored procedure. Creating Database Test cases helps you to identify the code defects which in-directly effects the application behavior. In order to create a database unit test, you need to VS studio 2010 test suite or ultimate version.

Before writing a unit test case , define a schema file and create a database project and  deploy to the database as shown below.

image

Schema file defines a table and one stored procedure to insert the data into that table.

image

Save the script file on to disk. Now Create a database project as shown below to test the script

image

Import the script into the project that we have created earlier by choosing a option import script file from project menu

image

Browse the file, click next and say finish.

Verify the script has been loaded into the project by expanding the schema objects in project explorer as shown below

image

Now deploy this scripts to database instance from the project by using the deploy option from properties window as shown below

image

Click the deploy solution from build menu and check the database for the deployed objects

image

Now let us go and create a database unit test by going to view menu and select Database Schema View then you will see the following window

image

Right click on the database object that you want to create a unit test as shown below

image

Name the project  and test class as shown below

image

Configure the database test project as shown below

image

When you click ok in above dialogue then it creates a unit test for stored procedure. Save all and build the database project. Edit the unit test to insert the record in to customer table as shown below. Change the test condition to row count.

image

Select the test case that you have created from test view and run the test case

image

See the test results in results view window and check the database whether row has been inserted or not

image

This is a very basic example of creating a database test case where you can create unit test cases on complex database objects and share with the other developers so that any change occur on database objects would not change the application behavior by running the test cases.

Share this post :

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

1 Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud