Run tSQLt Unit Tests
- Added the option to run only a Test Class or Test Case (Issue #6)
- Minor improvements on the task UI
With this build task, besides running tSQLt Unit Tests on a SQL Database, you'll also be able to:
- Export the test results to the build summary;
- Export the code coverage report to the build summary.
Originally, there is no build task that does all those 3 tasks. Thinking from an automation and engineering perspective, once you run your unit tests, it would be good to have its results published somewhere to inspect and adapt your tests.
This task relies on 3 components:
This is an Open Source component written by Ed Elliot to extract code coverage from a tSQLt test execution.
OpenCover to Cobertura Converter
An Open Source tool written by Daniel Palme to convert OpenCover code coverage reports to Cobertura, a coverage report supported by Azure DevOps. See more here.
Daniel Palme also wrote a tool to convert XML Code Coverage reports into human readable reports in various formats.
Basically, this extension performs the following tasks, in order:
- Run tSQLt using SQLCover;
- Exports the test results using TSQL Script;
- Save Stored Procedures source files for generating the Code Coverage report;
- Converts the Open Cover XML report into human readable report using Report Generator;
This extension is still in preview to gather information on using, feedback and tracking initial issues.
How to use it
If you just want your test (with or without Code Coverage) and you will not customize anything right now, just follow these steps:
- Add the Run tSQLt Unit Tests task;
- Add your connection string to the SQL Server Connection String field;
- Check Control Options/Continue on error;
- Add the Publish Test Results task;
- If you're enabling Code Coverage
- Add the Publish Code Coverage Results task;
- Change Code coverage tool to
- Change Summary file to
- Change Report directory to
Add the Run tSQLt Unit Tests build task to your pipeline
tsqlt and make sure to add the task to your pipeline:
The only important thing here is to highlight that the working directory for the task will be the build sources directory. You can change this but it's recommended to keep it as it is.
Here, two required information:
SQL Server Connection String: Make sure to test your connection string before using it here. A good source for connection strings is the Connection Strings website.
SQL Connection Timeout: It's recommended to use 60 (seconds) as a timeout for your connection. Be careful not to add an amount of time here that could make your build stuck on your queue.
You'll have default information filled here that is supposed to make it work by default with the requirements of the Publish Test Results task. You can also customize it.
The root folder to store all the output for this task: You must be aware that all the output from this task will be dropped under working $(System.DefaultWorkingDirectory)/tSQLt-out (or whatever you configure here).
The name for the Test Results XML report: This is the file that the Publish Test Results task will use to publish the results to the summary. Its default value is already discoverable by the Publish Test Results task's default configuration.
By default, Code Coverage is disabled. If you want the task to also generate Cobertura code coverage reports, you must enable it. Remember that all those paths are relative to working directory\root folder
- Folder to store the Open Cover generated source: It will be useful to show detailed code coverage report.
- The name for the Cobertura Code Coverage XML report: You'll use this format to Publish Code Coverage report later.
- HTML reports output: You'll need to publish this folder as an artifact so it will be shown as part of the build summary.
If any of your tests fail, the task will also fail. Because of that, you must mark the Continue on error option so your pipeline will publish test results and code coverage reports, if enabled.
Add the Publish Test Results to the pipeline
To have the test results available on the pipeline summary, you must publish it. To do that, you just need to use the Publish Test Results task:
Configure important parameters
- Test result format: The test results tSQLt provides is JUnit based.
- Test results file: The file name you configured on The name for the Test Results XML report.
- Search folder: It is the combination of the working directory and the root output. If you left everything default, it should be
Add the Publish Code Coverage Results task (if Code Coverage Enabled)
If you enabled Code Coverage on the Tests task, you'll need to publish its results to the pipeline summary.