By: Arshad Ali | 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:
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 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:
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.
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).
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:
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:
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:
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:
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:
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:
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.
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
- Review SQL Data Comparison with Visual Studio 2010
- Review SQL Schema Comparison with Visual Studio 2010
- Review Overview of Database Unit Testing
- Review my previous tips.
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips