TechBubbles Microsoft Technology BLOG

Unit Testing for SQL Server in Visual Studio 2013

Unit Testing is commonly leveraged in application development and this can applied to database development as well, In Visual Studio 2013 integrated database testing can run directly against the database.It can even be configured automatically in the team build scenario. This post outlines how to create database unit testing in Visual Studio 2013. Create a new SQL Server database project in Visual Studio 2013, to do the unit testing of a stored procedure go to SQL Server object explorer and under the projects node you will see the database that you are trying to write the unit test case, off course it is a logical view of your database

image

right click on the database stored procedure and say Create Unit Tests, The unit test case can be written in Visual Basic or C#. Give a name to your test project and click ok. The unit test that we are writing is test AddEmployee stored procedure.

image

It then prompt you to enter the database connection to execute the unit test case

image

you can also choose the secondary data connection for validating the test cases. One of the reason to use the secondary data connection is you might want to execute a procedure with one set of credentials and second set of credentials with more access level permissions to do the validation. You can also choose to deploy the database project before you run the unit test. This will deploy the database against your target connection before you running unit test. When you click ok it is going to generate a database unit test.

image

Notice your unit test contains a Execute statement call to your stored procedure.You can set the data that you want to pass in, before you add the record to database you might want to check if this record exist, this can be achieved using Pre-test  imageoption where you can create T-SQL script where it deletes the existing record.

image

To validate the test case , delete the default inconclusive test condition and add the row count condition for this unit test

image

set the row count for this condition to 1 for passing the test case.

image

After validating the above condition you might want to do some post test script with row condition set to 1like below to see the employee was actually inserted into the table.

image

Now build your project and your test case will appear in Visual Studio test explorer, right click on it and say run selected test case.

image

when you run it then connects to database and it then runs you pre-test, test and post test conditions and gives you the results. Database unit testing is a powerful tool in Visual Studio 2013 to validate the functionality at database level.

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

  • Thanks for the article. So if my unit test failed when I’m only expecting 1 row count return, but the test results had row count = 99 – Is there an option to review those 99 rows data ? Thanks.

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud