SQL Server Unit Testing with Visual Studio 2010

By:   |   Updated: 2010-12-16   |   Comments (4)   |   Related: > Testing


Problem

As a database developer you always want to ensure stored procedures, UDFs (User Defined Function) and triggers perform as expected. And more importantly you want to ensure that a change in an existing SP, UDF or trigger does not break the functionality. That is to say, you want to have smooth and fast regression testing for your database code. How would you do that, how would you write database Unit Test Cases (UTC)? In this tip we cover how you can do this in Visual Studio.

Solution

Database unit testing is used for feature testing of your individual modules (stored procedures, triggers or user defined functions) that is to say your module performs as expected. Apart from that, it is also used to ensure that subsequent changes to the module does not break any functionality.

At first glace, it looks like this would add overhead to create vs. doing adhoc testing, but Visual Studio lets you automatically generate T-SQL code stubs to test the database object which you can customize as per your need.

Visual Studio provides Database Unit Test Designer which you can write/define T-SQL scripts (also insert SQL assertion in this code) that calls your module and then evaluates the execution result against the different test conditions which indicates your modules execution success or failure.

Note: In this demonstration I am going to show how you can create Database Unit Test cases in Visual Studio 2010 Ultimate edition although you can do the same with Visual Studio 2005/2008 Database edition too.

Example

Open the Microsoft Visual Studio 2010 IDE (Integrated Development Studio) and select the Test Project template under Test Projects->Test Documents page as shown below. Specify the name of your unit testing project and location where you want to create the project, finally click on the OK button to create the project:

Visual Studio lets you automatically generate T-SQL code stubs to test the database object

In Solution Explorer you can see the created project with a default unit test created, delete this unit test (because this is not a database unit test), right click on the project, go to Add and select "New Test..." as shown below:

in solution explorer you can see the created project

go to add and select new test

In the Add New Test dialog box, select the Database Unit Test template and specify the name of the database unit test which you are creating:

 select the Database Unit Test template

When you click on the OK button (for the first time) in the above screen, a new dialog box will appear as shown below. Here you need to specify the database connection string which will be used as the target to execute the created database unit test cases. You can also specify to deploy the database project to your target database if one exists in your current solution and you can specify to generate test data for your database too.

you need to specify the database connection string

With each database unit test case generated, you might have Pre-test and Post-test cases included to ensure the environment is setup before actual test execution (Pre-test) and its cleaned up after the execution (Post-test).

you might have Pre-test and Post-test cases included

For example before executing a test case for a stored procedure I want to ensure it exists or raise an exception as you can see below:

before executing a test case for a stored procedure I want to ensure it exists or raise an exception

There are two ways to ensure successful execution of your test, first using the T-SQL assertion in your test case itself or second using one or more of the available test conditions as shown below:

first using the T-SQL assertion in your test case itself or second using one or more of the available test conditions

For example in my case, I am calling uspGetEmployeeManagers stored procedure and passing employee id 143. I know this should return 4 records, if not then the test case should fail, this is what I have done below. I have used the RowCount test condition and specified 4 in the Properties window for total row count:

use the RowCount test condition and specify 4 in the Properties window for total row count

Now that we have created our first database unit test case, we need to execute it. There are different ways to execute the test cases, I frequently use the Test List Editor. To launch the Test List Editor, go to Test -> Windows -> Test List Editor as shown below:

 use the Test List Editor

Test List Editor allows you to group the test cases as per your need in different lists and those lists are shown on the left side. On the right side you can view the test cases which you created, choose as many of them as you want to execute and click on the "Run Checked Tests" icon from the tool bar above:

click on the "Run Checked Tests" icon from the tool bar

A new window called "Test Results window" will be opened to show the results of the test cases execution. During execution the status would be pending and after execution it will show the success or failure result. If you have Inconclusive results it means you have not edited your test case and you need to that. Double click on the failure message to see the details of the failure. You can see below my test case failed because it was expecting 4 records to be returned, but it returned 3 records:

"Test Results window"

Now let's go to SSMS and execute the same query and see the result. Here you can see that it returned 3 records only and this is how you align your test cases with code written and vice versa and grab the cribbed in errors in your code.

go to SSMS and execute the same query and see the result

Note: When you add a database unit test case you will notice one Inconclusive test condition is automatically added by default, this is to remind you to add a T-SQL script with SQL asserts or to add other test conditions.

This was a simple test to show you how you can get started with this. Take the time to build some test cases for your SQL Server code.

Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Arshad Ali Arshad Ali is a SQL and BI Developer focusing on Data Warehousing projects for Microsoft.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2010-12-16

Comments For This Article




Thursday, December 5, 2013 - 1:50:49 AM - Chandan Back To Top (27695)

I tried to use the DB Unit testing against Teradata, but looks i cant add other than MS SQL server in the new connection.

Could you please help me.


Tuesday, December 6, 2011 - 11:11:50 AM - Oladeji Sadiq Back To Top (15300)

I need to develop database application system that will allow me to do the following, usind SQL Server AND Visual Studio 2005 or later:

  1. Download data(like txt file, etc) from the internet and process the download for database storage.
  2. Develop desktop application that will allow different network clients to work on the downloaded data.
  3. Process the downloaded that for printing on pre-printed hard paper like receipt or certificate.
  4. Communicate with the SQL Server database using desktop application develop with the visual studio. etc

I will appreciate anyone who can give me step by step guide on how to set up the can of SQL Server database project and development of network enable desktop application develop in visual studio 2005 or later, thank you.


Monday, October 3, 2011 - 1:49:52 PM - Rafael Back To Top (14766)

*** NOTE *** - If you want to include code from SQL Server Management Studio (SSMS) in your post, please copy the code from SSMS Hello!

Is it possible to somehow see the results of TSQL execution after the test runs?  This is instead of copying the code into a separate TSQL window and running it.

Thank you in advance!

 


Thursday, January 6, 2011 - 5:23:50 PM - Dennis Back To Top (12524)

Is there a way to add database unit test project templates to VS2010 Professional?

 















get free sql tips
agree to terms