Intro to Unit Testing with tSQLt Sandra Walters BTEK Software, Inc. Intro to Unit Testing with tSQLt
Agenda Why should I write unit tests? …SQL unit tests?! How? Using tSQLt in SQL Management Studio The tSQLt unit test adapter for Visual Studio SSDT projects with tSQLt ReadyRoll projects with tSQLt CI with tSQLt tests and VSTS Code Coverage with SQL Cover
Bugs found by unit tests are the cheapest to fix Source: . How Google Tests Software. Westford, MA: Addison-Wesley, 2012.
Why write unit tests? Helps team agree on “working as intended” Instant feedback to the developer Does refactored code behave well? Defines expectations (when used with TDD)
We’ve all seen this meme, right. We’ve all worked with this guy We’ve all seen this meme, right? We’ve all worked with this guy. Don’t be this guy.
What is tSQLt? Database unit testing framework MS SQL 2005 SP2 and up, all editions Unit tests, and asserts, written in T-SQL Tests are run within transactions Tests are grouped within schemas Fake tables and views Stored procedure “spies”
http://tsqlt.org Source: https://github.com/tSQLt-org/tsqlt
New in SQL 2017 Server-level option: CLR strict security (on by default) How to enable the tSQLt CLR to be installed: EXEC sp_configure ‘show advanced options’, 1 RECONFIGURE; EXEC sp_configure ‘clr strict security’, 0 NOT FOR SECURE / PRODUCTION ENVIRONMENTS… but then, you really shouldn’t be unit testing there anyway. Further reading: https://github.com/tSQLt-org/tSQLt/issues/25
Assert methods available in the tSQLt schema AssertEmptyTable AssertEquals AssertEqualsString AssertEqualsTable AssertEqualsTableSchema AssertLike AssertNotEquals AssertObjectDoesNotExist AssertObjectExists AssertResultSetsHaveSameMetaData Fail
How to define tSQLt unit tests Create a new test class (schema) tSQLt.NewTestClass ‘class name’ Create unit tests (stored procedures) in the test class; must start with the word ‘test’ CREATE PROC [ClassName].[Test something]…. Create a unit test setup (stored procedure) if necessary; must be named ‘Setup’ CREATE PROC [ClassName].[SetUp]…
How to run tSQLt unit tests Run all tests tSQLt.RunAll Run a single unit test tSQLt.Run ‘TestName’ Run all tests in a test class tSQLt.Run ‘ClassName’
Using SQL Test
Sample Database: Wide World Importers Source: https://github.com/Microsoft/sql-server-samples
Demo tSQLt added to an existing database Running tSQLt unit tests in SQL Management Studio Creating new tests
SSDT Projects with tSQLt “Composite” Projects: Main database project Subordinate projects (such as tSQLt unit tests)
Unresolved references! Subordinate projects: set the referred project’s location as “Same Database”
Running tSQLt Unit Tests in the VS IDE
Specify test SQL database connection in a runsettings file:
Test Explorer with tSQLt unit tests found
ReadyRoll Core in VS2017 Enterprise
Creating a ReadyRoll Core Project in VS2017 ‘Wizard’-like walkthrough to create project from existing database:
Imported list of objects:
Continuous Integration with tSQLt tSQLt Test Adapter must be added to the solution (3 possible ways) Place binaries directly on the build server in a folder accessible by the build agent Add the binaries to a solution folder within the Visual Studio solution Add the NuGet package for the test adapter to any project within the solution that can accept NuGet packages (no SSDT or ReadyRoll projects)
At a minimum, the build must: Push the revised unit tests to a SQL database Run the unit tests on the database
Pushing unit tests to a SQL database via a Command Line task with SqlPackage
Running the unit tests against a SQL server during the build At a minimum, set these items: Test assemblies Settings file Path to custom test adapters
Results from the test task – Success!
Code Coverage with SQL Cover Source: https://github.com/GoEddie/SQLCover
SQLCover uses the SQL Server API to determine what is “coverable”
Why use code coverage? Source: https://martinfowler.com/bliki/TestCoverage.html
tSQLt is found at: http://tsqlt.org/ Blog articles based on this talk: http://blog.bteksoftware.com/ Article from the tSQLt creators: https://www.red-gate.com/simple-talk/sql/t-sql-programming/sql-server- unit-testing-with-tsqlt/ Author of the Visual Studio tSQLt Test Adapter and SQLCover: https://the.agilesql.club/blogs/Ed-Elliott/ RedGate – introduction to SQL Cover: https://www.red-gate.com/blog/sql-cover Microsoft's Wide World Importers sample database (and others): https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world- importers-v1.0
Thank you Sponsors