views:

91

answers:

5

I need to understand and clarify some T-SQL stored procedures that contain important business logic. Does anyone know of any tools, tips, tricks, or hints that can be used for unit testing so I can refactor this sql without breaking it?

+1  A: 

Simon Harriyott suggests using TSqlUnit for this. There is more information about his process on his blog.

Robert Harvey
+1  A: 

why do you need to refactor?

Add a bunch of PRINT statements and manually run it from the Management Studio. If you print the proper info, you should be able to trace through the code and understand what is going on.

If you don't know what parameters to use, modify it to send the input parameters to a log file, then use those to run it manually.

KM
Why not use the debugger?
John MacIntyre
@John MacIntyre, what debugger?
KM
@KM-If you are using SQL2005 or above, VS is installed which you can debug from. If you are using SQL2000, there is a debugger built into Query Analyzer.
John MacIntyre
+1  A: 

If you have access to Visual Studio Team System Database Edition, then it comes with a bunch of unit-testing tools geared specifically for databases, as explained in this MSDN article.

tbreffni
+1  A: 

I use C# for unit testing stored procedures - that is way more efficient than T-SQL. The first link explains how to quickly generate unit tests.

Close those Loopholes - Testing Stored Procedures

Close These Loopholes - Testing Database Modifications

AlexKuznetsov
+1  A: 

For help on refactoring itself (as opposed to getting it under test so you can refector), do not forget to check out Refactoring Databases (either the book or the website).

I did write tests for stored procedures with the calling code (in my case Java with dbunit), but I do not know how it would compare against other methods.

Kathy Van Stone