For those of you using VSTS for Database Professionals or Team Suite, you have the ability to create unit tests for your database stored procs, functions, views, and constraints. Sachin Rekhi has posted a nice little whitepaper on how to get started with Database Unit Testing.
I find writing DB unit tests much like writing them in C# except for one thing that threw me a curve ball. Besides the main test code, database tests have a 'pre-test' and 'post-test' code sections. But, unlike the [TestInitialize] and [TestCleanup] designated methods in .NET testing that are specified once but run the same for every test method in the test class, 'pre-test' and 'post-test' SQL scripts are specific for each test script. So, if you need to initialize some data in the database, you'll need to copy the 'pre-test' script for each individual test. Likewise, you'll need a copy of the 'post-test' script for every one, too. It seems like there should be an area to factor out common setup and cleanup SQL code for database testing, too.
Posted by: Todd Girvin | 2007.09.03 at 10:27 PM