Did you know that you can write database unit tests for SQL Server?
There is a framework called tSQLt that helps you to do it.
What about using tests for learning TSQL?
I think that it is not unreasonable to design a TSQL course where each lesson is made of a set of tests that you need to make pass. This idea of learning by making tests pass is used in the nand2tetris course that I am doing with the friends of the Cambridge Programmer’s Study Group.
In this post, I try to create an example using tSQLt.
The database I use is from the Training Kit: Querying Microsoft SQL Server 2012.
First run the following SQL statements.
Execute the tSQLt.class.sql script provided by tSQLt.
Creating the test
First, you need to create a test class that we call Tests.
Then, we create a test.
The framework tSQLt allows you to create fake tables but in this case the test use the actual table HR.Employees and the actual data contained in it.
Running the test
Make the test pass and learn