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.
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.
Add a dependency to the 03 – Test configuration.
Add a trigger to the 03 – Test configuration.
Run 01 – Build & it will trigger both 02 – Deploy & 03 – Test on completion, deploying both databases to the Tentacle server & running the 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.
Click on the test to see more detail (e.g. the name of the test that has failed).
Check the build log for the output of the failing test.
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;"