Unit tests should be treated the same as application code, meaning they should be contained within a project under source control. Being under source control will allow the tests to be maintained and changes tracked when multiple people are helping to write tests. Also having the unit test in a project means there is a deployment mechanism out-of-the-box.
The tsqlt framework has a few requirement which are as follows
- CLR is enabled on the SQL Server
- The framework and the unit test are contained within the same database of the objects which are being tested
- The database instance is trustworthy
However one question would be raised which is how do you stop the unit test framework and the unit tests getting into the production deployment? The tsqlt framework does offer an approach to this problem by supplying an uninstall store procedure which could be called after deployment to remove the unit tests and the framework. This is more of a workaround rather than solving the problem of not deploying tests and the framework into production.
There is an alternative solution, which is the method I used, to integrate the unit tests into the project life cycle. The solution is to use SSDT project setup method of composite database project. The composite database project method, which has been around since visual studio database professionals, is basically having 2 (or more) database projects, where one references the other(s), which both gets deployed into the same database on the same server.
My method to implement composite SSDT database project was as follows:
- Created an application database project as normal. Which contained all the objects for the application requirements.
- Create a unit tests database project to contain all my unit tests and supporting objects. This project could also be where tsqlt unit framework code could be stored. I decided created a separate tsqlt dacpak file by using the following steps:
- Create an empty database.
- Execute the tsqlt install script within the empty database.
- Use SQLPackage with the extract action.
- Before starting to create my unit test code within my unit to project I had to set up the references. I created references to the application project and the tsqlt dacpack file but setting database location to be “same database”, which is the same as removing the generated database name and variable.
The reason for creating a dacpack file was to have a single reusable file for my next projects. And if the unit framework is updated I only have to update a single file. Also hopefully at some point the SSDTS project will support the use of NuGet. This would mean that I can upload my dacpack to a NuGet server and add the package reference. Then if I ever updated the server package the project would automatically pick up the new version. (There is a fork within the NuGet project which already allows this: https://nuget.codeplex.com/workitem/2439.)
Here is a link to find out more about composite projects: http://blogs.msdn.com/b/ssdt/archive/2012/06/26/composite-projects-and-schema-compare.aspx.
SQLpackage has a setting which will allow a composite project to deploy its composite references objects at the same time. This allow the unit test project to deploy the unit tests including the application database and the tsqlt framework database objects. However be aware that only one Pre/Post deployment file is executed which is one that is attached to the dacpack listed in the SQL package source file parameter. So the composite references project have post/pre-deployment requirements are not executed. This can be a problem if your application database has a post-deployment script to load reference data.
A couple of closing notes:
To create a unit test class within SSDTS for tsqlt add the following extended property on the schema which is to become a tsqlt test class: tSQLt.TestClass with a value of 1
1: EXECUTE sp_addextendedproperty
2: @name = N'tSQLt.TestClass'
3: , @value = 1
4: , @level0type = N'SCHEMA'
5: , @level0name = N'MyUnitSchema';
And creating unit test the store procedure must start with "test "