tSQLt Part 3: Running tests from TeamCity

Aim: Automatically run the tSQLt tests from TeamCity following each successful build & deploy.

Fortunately, there is a TeamCity runner for tSQLt available at https://github.com/cprieto/tsqlt-teamcity which makes running tSQLt tests from TeamCity very easy.

Add a step to run the tests:
Download the latest version of the tSQLt runner from: https://github.com/cprieto/tsqlt-teamcity/releases.  Via Services, stop the TeamCity Server.  Paste tsqlt-runner.zip to C:\ProgramData\JetBrains\TeamCity\plugins. Start the TeamCity Server again.  In the TeamCity interface, check Administration > Plugin list for the new plugin.
30 tc_tsqlt_plugin
In SSMS, create a new SQL login (e.g. TestLogin) which is the login used by TeamCity to run the tSQLt tests.  Give the login the required permissions to run the tSQLt tests.

In the TeamCity interface, create a new build configuration (03 – Test). Add a “tSQLt Runner” build step to the 03 – Test configuration.
31 tc build_step_3
Add a dependency to the 03 – Test configuration.
32 tc_dependencies_2
Add a trigger to the 03 – Test configuration.
33 tc_trigger_2
Run 01 – Build & it will trigger both 02 – Deploy & 03 – Test on completion, deploying both databases to the Tentacle server & running the tests.
34 tc_success_tests

Add a breaking test:
Add a breaking test to the Accounts_Tests project, add it to version control & run from 01 – Build again to see how TeamCity handles the failure.
35 tc_failed_test
Click on the test to see more detail (e.g. the name of the test that has failed).
36 tc_failed_test_detail
Check the build log for the output of the failing test.
37 tc_failed_test_build_log

Add a step to drop the test database:
This is an optional step but I think it’s useful to keep the environment clean after the tests have been run. Add a new “Command Line” build step to the 03 – Test configuration.  Add the following commands to drop all connections & drop the Accounts_Tests database. If using TestLogin to drop the database, you may need to increase its permissions.

sqlcmd -U “TestLogin” -P <password> -S <ServerName> -Q "ALTER DATABASE Accounts_Tests SET SINGLE_USER WITH ROLLBACK IMMEDIATE;"
sqlcmd -U “TestLogin” -P <password> -S <ServerName> -Q "DROP DATABASE Accounts_Tests;"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Kevin Kline

Career and Technical Advice for the IT Professional


Explaining the bits and bytes of SQL Server and Azure

SQL Authority with Pinal Dave

SQL Server Performance Tuning Expert


A blog about PowerShell and general Windows sysadmin stuff

Simon Learning SQL Server

I'm trying to become "better" at SQL Server and data - here's how I'm doing it!

%d bloggers like this: