Chapter 27. Writing unit tests for Transact-SQL
Unit testing is a known methodology among developers around the world, be they Java, C#, VB.NET, or C++ programmers. Unit testing helps you produce code with fewer bugs and achieve a higher degree of quality. Although this has become a well-known practice, when it comes to databases, we haven’t seen the same adoption rate.
The objective of this chapter is to introduce some basic concepts of unit testing that you can apply when programming with T-SQL, without using any third-party tool or external framework. I’ll show you how to make the necessary adjustments and bend some rules to adapt unit tests to databases.
The primary goal of unit testing is to take the smallest piece of testable code from your project and write tests to verify that the code behaves exactly as it’s supposed to. This behavior is usually driven by the parameters you provide as the input for the call, expecting an output from the callable code (usually a function or method). This outcome may be a return value, an exception (yes, it can be the right output), or even some changes in the state of the object, a term in object-oriented programming that represents a change in an object property.
Here are some of the benefits of unit tests: