![Page 1: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/1.jpg)
ADDING UNIT TESTS WITH TSQLT TO THE DATABASE DEPLOYMENT PIPELINE
Eduardo Piairo
@EdPiairo
#sqlbits
![Page 2: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/2.jpg)
ADDING UNIT TESTS WITH TSQLT TO THE DATABASE DEPLOYMENT PIPELINE
Eduardo Piairo
@EdPiairo
#sqlbits
![Page 3: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/3.jpg)
ABOUT MEAdding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #sqlbits
@EdPiairo
https://pt.linkedin.com/in/eduardopiairo
https://www.eduardopiairo.com
Eduardo Piairo
DevOps Coach @ Natixis | DevOps Porto Co-Founder |Friend of Redgate
![Page 4: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/4.jpg)
TSQLTAdding unit tests with tSQLt to the database deployment pipeline
• Database unit testing framework for Microsoft SQL Server
• Allow to write T-SQL code as tests
• Tests are automatically run within transactions
• Provides a way to isolate code and tables using mocking
• Output can be plain text or XML
@EdPiairo, #sqlbits
![Page 5: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/5.jpg)
TSQLT INSTALLAdding unit tests with tSQLt to the database deployment pipeline
• tSQLt.class.sql
• CLR
• clr enabled
• clr strict security
• Should be installed in the development database
@EdPiairo, #sqlbits
![Page 6: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/6.jpg)
WHAT’S A TSQLT TEST? Adding unit tests with tSQLt to the database deployment pipeline
• Stored Procedure
• Starts with the word test
• Must be in a schema that contains the extended property tSQLt.TestClass = 1
• tSQLt.NewTestClass
• Each test is wrapped in a transaction
• Modifications are rolled back and the results saved
@EdPiairo, #sqlbits
![Page 7: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/7.jpg)
WHAT’S A TSQLT TEST? Adding unit tests with tSQLt to the database deployment pipeline
• Benefits
• Business requirements documentation
• Code refactoring
• Isolation - unrelated changes do not affect other parts of the system
• Help structure code into distinct components – keep it small
@EdPiairo, #sqlbits
![Page 8: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/8.jpg)
TSQLT CREATEAdding unit tests with tSQLt to the database deployment pipeline
EXEC tSQLt.NewTestClass my_new_test_class’;
GO
CREATE PROCEDURE my_new_test_class.[test something important]
AS
BEGIN
-------Assemble
--This section is for code that sets up the environment
-------Act
-- Execute the code under test like a stored procedure, function or view
-- and capture the results in variables or tables.
-------Assert
-- Compare the expected and actual
END;
@EdPiairo, #sqlbits
![Page 9: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/9.jpg)
TSQLT RUNAdding unit tests with tSQLt to the database deployment pipeline
• tSQLt.RunAll
• Execute all the tests
• tSQLt.Run '[your_test_class].[your_test]
• Execute a specific test
• tSQLt.Run '[your_test_class]'
• Execute a specific test class
@EdPiairo, #sqlbits
![Page 10: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/10.jpg)
WHAT CAN BE TESTED? Adding unit tests with tSQLt to the database deployment pipeline
• Stored Procedures
• Functions
• Views
• Tables
• Tables constrains that are critical
@EdPiairo, #sqlbits
![Page 11: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/11.jpg)
TESTING (WHITOUT DATA)Adding unit tests with tSQLt to the database deployment pipeline
• Unit tests is about testing code
• You do not need a database full of data, you need the opposite
• Makes creating unit tests easy
• Data setup
• Only the necessary data for making the test work is needed
• Mocking
• tSQLt.FakeTable
• tSQLt.FakeFunction
• tSQLt.SpyProcedure
![Page 12: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/12.jpg)
ISOLATING OBJECTSAdding unit tests with tSQLt to the database deployment pipeline
• tSQLt.FakeTable
• Fakes the original table without constrains
• Isolate the table from constrains – I don’t need unnecessary data
• tSQL.FakeFunction
• Simply replaces the original function
• Allow to simplify the logic
• tSQLt.SpyProcedure
• Replace the original SP with a spy
• The spy will record the parameters that were passed to it@EdPiairo, #sqlbits
![Page 13: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/13.jpg)
ISOLATING OBJECTSAdding unit tests with tSQLt to the database deployment pipeline
• ApplyConstraint
• RemoveObjectIfExists
• ApplyTrigger
• RemoveObject
@EdPiairo, #sqlbits
![Page 14: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/14.jpg)
ASSERTSAdding unit tests with tSQLt to the database deployment pipeline
• tSQLt.AsserEquals
• tSQLr.AssertEqualsTable
• tSQLt.AssertEmptyTable
• tSQLt.AssertEqualsString
• tSQLt.AssertEqualsTableSchema
• tSQLt.AssertLike
• tSQLt.AssertNotEquals
• AssertObjectDoesNotExist
• AssertObjectExists
• AssertResultSetsHaveSameMetaData
• Fail
@EdPiairo, #sqlbits
![Page 15: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/15.jpg)
SQLTESTAdding unit tests with tSQLt to the database deployment pipeline
• Redgate SQL Test
• Add-in for SSMS for creating and running unit tests
• Measure the code coverage of those tests
@EdPiairo, #sqlbits
![Page 16: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/16.jpg)
ADDING TSQLT TO THE DEPLOYMENT PIPEPLINEAdding unit tests with tSQLt to the database deployment pipeline
Source
ControlContinuous
Integration
Continuous
Delivery
@EdPiairo, #sqlbits
![Page 17: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/17.jpg)
ADDING TSQLT TO THE DEPLOYMENT PIPEPLINEAdding unit tests with tSQLt to the database deployment pipeline
Source
ControlContinuous
Integration
Continuous
Delivery
@EdPiairo, #sqlbits
![Page 18: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/18.jpg)
SOURCE CONTROLAdding unit tests with tSQLt to the database deployment pipeline
• GitHub
• T-SQL migrations
• tSQLt tests
• Pester tests
• Building scripts
@EdPiairo, #sqlbits
![Page 19: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/19.jpg)
CONTINUOUS INTEGRATIONAdding unit tests with tSQLt to the database deployment pipeline
• VSTS
• TEST environment setup (Local Machine | Docker)
• Test and Report
• Build artefact
@EdPiairo, #sqlbits
![Page 20: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/20.jpg)
CONTINUOUS DELIVERYAdding unit tests with tSQLt to the database deployment pipeline
• Octopus Deploy
• Local Machine
• Azure
@EdPiairo, #sqlbits
![Page 21: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/21.jpg)
REFERENCES & MATERIALSAdding unit tests with tSQLt to the database deployment pipeline
• References
• https://tsqlt.org/user-guide/
• https://courses.agilesql.club/
• Source code available @ GitHub
• https://github.com/eduardopiairo/devopsporto-db
@EdPiairo, #sqlbits
![Page 22: ADDING UNIT TESTS WITH TSQLT TO THE DATABASE …...Adding unit tests with tSQLt to the database deployment pipeline •Unit tests is about testing code • You do not need a database](https://reader033.vdocuments.site/reader033/viewer/2022043019/5f3bb21eb7122c1aff670b85/html5/thumbnails/22.jpg)
Q&AAdding unit tests with tSQLt to the database deployment pipeline
@EdPiairo
https://pt.linkedin.com/in/eduardopiairo
https://www.eduardopiairo.com
@EdPiairo, #sqlbits