The Many Ways to Execute Database Unit Tests

I wanted to make sure you were all aware of the various methods of executing database unit tests. I’ve talked with some users who were pleasantly surprised to learn of several of the other options, so I thought I’d throw it up on my blog for everyone’s delight. These should help for those trying to quickly and efficiently implement the dev\test cycle that you should be doing with every change you make to your schema objects.


Top Level Test Menu Items

You can use the following two top-level menus to execute tests:

§ Test -> Start Selected Test Project with Debugger (Shift+Alt+X)

§ Test -> Start Selected Test Project without Debugger (Ctrl+Shift+X)


To use these, the test project containing the tests which you wish to run must be the active project in Solution Explorer. These commands will execute all the tests inside the currently selected test project.


Test View Window

You can use the Test View to quickly run specific tests. To bring up the Test View Window, go to Test -> Windows -> Test View. This brings up a nice little window that you can dock side-by-side with, say, Solution Explorer. Then you simply select the tests which you wish to run, right-click, and say “Run Selection”. The Test View also has simple filtering capabilities, so you can easily find the specific test you are looking for.


Test Manager Window

The Test Manager can also be used for executing database unit tests. The Test Manager provides a more sophisticated view of your tests and some test case management capabilities. To open the Test Manager, select Test -> Windows -> Test Manager. The Test Manager offers the same capabilities of the Test View and more. You can check a set of tests that you wish to run and execute them. More importantly, you can define tests lists and execute the tests list as a single unit. This is very useful for setting up check-in test suites that you ask your developers to execute prior to every check-in.


Test Results Window

The Test Results window appears whenever you execute tests to report to you the status of those tests. If a test has failed, it’s very easy to re-execute the test from the Test Results Window. Simply select the test and press the run toolbar button.



You can make the process of making database changes and the cycle of build, deploy, generate test data, and re-run tests very simple through the use of F5.


To set this up, make the test project the startup project. Right-click on the test project and select “Set as Startup Project”. Then go to Tools -> Options -> Projects and Solutions -> Build and Run and check “Only build startup projects and dependencies on Run”. Then ensure that you have configured in Test -> Database Test Configuration to automatically deploy and generate test data.


Now whenever you make changes to your database schema, simply select the test project in Solution Explorer and press F5! The database project will be built and deployed, test data will be generated, and your tests will be re-run. This makes iterative development and testing very simple.



You can checkout a previous blog post that describes just how to go about using the mstest.exe command-line tool to execute database unit tests.



And here is a link to the MSDN Documentation drilling in deeper on running Tests.


Hope that helps!


Sachin Rekhi