Friday 23 March 2012

tSQLt - Writing tests

I am not going to go into detail on how to download and install tSQLt here as it is pretty straight forward and all the instructions are on the tSQLt website. 
It's worth creating a test database in order to try out the examples in the code below.  Once you have created the test database follow the instructions on the tSQLt website to install tSQLt on this database.
Once tSQLt is installed the obvious starting point would be to write and run a test.


tSQLt introduces a concept of a TestClass.  This is a class that is used to group a set of unit tests. tSQLt uses schema's to accomplish this.  I am currently using the following naming convention UnitTests_<schemaName> where the schemaName is the name of the schema that contains the code that is being tested.
Creating a TestClass:

EXEC tSQLt.NewTestClass 'UnitTests_Processing'

Use this query to view your TestClasses:

SELECT * FROM tSQLt.TestClasses

And to delete a TestClass

EXEC tSQLt.DropClass 'UnitTests_Processing'
You actually don't need to create a TestClass as shown above.  You can just create a normal schema and start writing tests against that schema.  The downside of doing this is that the schema won't be correctly registered with tSQLt and commands like

EXEC tSQLt.RunAll
won't run these tests.

Writing a test is as simple as creating a stored procedure within a test schema (TestClass).  All test stored procedures need to start with the word "test". Test names can include spaces.

CREATE PROCEDURE [UnitTests_Processing].[test 1 should equal 1]
AS
BEGIN
-- this is the simplest example of an assert
    EXEC tSQLt.AssertEquals 1, 1, '1 should equal 1'
END

That is it for now.

Friday 16 March 2012

Database unit testing with tSQLt

I am currently working on a project that has a fair amount of backend database processing happening.  I have got a fair amount of TDD experience in a C# environment and have found that once you get into TDD it becomes a way of life.  As such, I wanted to apply the same approach to my database development. 

After a bit of researching I stumbled across an Open Source unit testing framework for SQL Server called tSQLt.  I have been using it for a few weeks now and yesteday I past the 100 test mark in the project I am working on.  It took me a little while to get up and running with some of the concepts and idea's around tSQLt unit testing, but I am already feeling very comfortable writing database unit tests using tSQLt.

There is a very good series of blog posts on tSQLt already at http://datacentricity.net/tag/tsqlt/
Over the next few days (I hope).  I will try to put together a series of post's showing the basics working of each test type without any extra logic to get in the way of what is going on.

Welcome

It's Friday morning.  I got my headphones on with the sound track to Pulp Fiction playing... and I have just created my new blog.  My plan for this blog is to publish blog post's related to the programming problems that I encounter from day to day.

My aim is that this will act as a repository of sorts so that when I encounter the same issues in the future I know where to start looking.  If what I blog about manages to help anyone out there, that's just a bonus.