SlideShare a Scribd company logo
ADDING UNIT TESTS WITH TSQLT TO THE
DATABASE DEPLOYMENT PIPELINE
Eduardo Piairo
@EdPiairo
#TugaIT
ABOUT ME
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
@EdPiairo
https://pt.linkedin.com/in/jesuspiairo
eduardopiairo@gmail.com
http://www.eduardopiairo.com/
Eduardo Piairo
DevOps Coach @ Natixis
DevOps Porto Founder
Friend of Redgate
TSQLT
Adding 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, #TugaIT
TSQLT INSTALL
Adding 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, #TugaIT
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, #TugaIT
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, #TugaIT
TSQLT CREATE
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
EXEC tSQLt.NewTestClass my_new_test_class’;
GO
CREATE PROCEDURE my_new_test_class.[test something imortant]
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;
TSQLT RUN
Adding 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, #TugaIT
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, #TugaIT
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
@EdPiairo, #TugaIT
ISOLATING OBJECTS
Adding 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, #TugaIT
ISOLATING OBJECTS
Adding unit tests with tSQLt to the database deployment pipeline
• ApplyConstraint
• RemoveObjectIfExists
• ApplyTrigger
• RemoveObject
@EdPiairo, #TugaIT
ASSERTS
Adding 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, #TugaIT
SQLTEST
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
• Redgate SQL Test
• Add-in for SSMS for creating and running unit tests
• Measure the code coverage of those tests
ADDING TSQLT TO THE DEPLOYMENT PIPEPLINE
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
Source
Control
Continuous
Integration
Continuous
Delivery
ADDING TSQLT TO THE DEPLOYMENT PIPEPLINE
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
Source
Control
Continuous
Integration
Continuous
Delivery
SOURCE CONTROL
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
• GitHub
• T-SQL migrations
• tSQLt tests
• Pester tests
• Building scripts
CONTINUOUS INTEGRATION
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
• VSTS
• TEST environment setup (Local Machine | Docker)
• Test and Report
• Build artefact
CONTINUOUS DELIVERY
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo, #TugaIT
• Octopus Deploy
• Local Machine
• Azure
REFERENCES
Adding unit tests with tSQLt to the database deployment pipeline
• https://tsqlt.org/user-guide/
• https://courses.agilesql.club/
@EdPiairo, #TugaIT
Q&A
Adding unit tests with tSQLt to the database deployment pipeline
@EdPiairo
https://pt.linkedin.com/in/jesuspiairo
eduardopiairo@gmail.com
http://www.eduardopiairo.com/

More Related Content

PDF
Adding unit tests to the database deployment pipeline
PDF
Adding unit tests with tSQLt to the database deployment pipeline
PDF
Adding unit tests to the database deployment pipeline
PDF
Adding unit tests with tSQLt to the database deployment pipeline
PPT
API Performance Testing
PDF
Testing in Ballerina Language
PPTX
Bug zillatestopiajenkins
PDF
Python unittest
Adding unit tests to the database deployment pipeline
Adding unit tests with tSQLt to the database deployment pipeline
Adding unit tests to the database deployment pipeline
Adding unit tests with tSQLt to the database deployment pipeline
API Performance Testing
Testing in Ballerina Language
Bug zillatestopiajenkins
Python unittest

What's hot (19)

PPT
Qtp manual testing tutorials by QuontraSolutions
PDF
Benchmarking at Parse
PDF
QA Meetup at Signavio (Berlin, 06.06.19)
PPTX
Quickly Testing Legacy C++ Code with Approval Tests
PDF
Kirill Rozin - Practical Wars for Automatization
PDF
SUnit: latest developments
PPTX
Nautilus
PPTX
PostgreSQL and JDBC: striving for high performance
PPTX
Ensuring Software Quality Through Test Automation- Naperville Software Develo...
PPTX
Test it! Unit, mocking and in-container Meet Arquillian!
PPTX
TestNG with selenium
PPTX
Introduction to JUnit testing in OpenDaylight
PPTX
TestNG Session presented in Xebia XKE
PDF
PDF
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
PDF
Test ng for testers
PPT
20111018 boost and gtest
PDF
Testing Spring Boot Applications
PDF
CICD Pipeline configuration as a code
Qtp manual testing tutorials by QuontraSolutions
Benchmarking at Parse
QA Meetup at Signavio (Berlin, 06.06.19)
Quickly Testing Legacy C++ Code with Approval Tests
Kirill Rozin - Practical Wars for Automatization
SUnit: latest developments
Nautilus
PostgreSQL and JDBC: striving for high performance
Ensuring Software Quality Through Test Automation- Naperville Software Develo...
Test it! Unit, mocking and in-container Meet Arquillian!
TestNG with selenium
Introduction to JUnit testing in OpenDaylight
TestNG Session presented in Xebia XKE
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
Test ng for testers
20111018 boost and gtest
Testing Spring Boot Applications
CICD Pipeline configuration as a code
Ad

Similar to Adding unit tests with tSQLt to the database deployment pipeline (20)

PDF
A data driven etl test framework sqlsat madison
PDF
Agile db testing_techniques
PDF
Test Driven Development with Sql Server
PDF
Database development unit test with tSQLt
PDF
Test Driven Development with JavaFX
PPTX
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
PDF
Unit Testing SQL Server
PDF
Just Do It! ColdBox Integration Testing
PPTX
Get Testing with tSQLt - SQL In The City Workshop 2014
PPTX
Qt test framework
 
PDF
POUG2019 - Test your PL/SQL - your database will love you
PPTX
Introduction to SoapUI day 2
PDF
Bgoug 2019.11 test your pl sql - not your patience
PPTX
1.Python_Testing_Using_PyUnit_Pytest.pptx
PPTX
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
PDF
How to integrate_custom_openstack_services_with_devstack
PDF
QuerySurge for DevOps
PDF
PaaSTA: Running applications at Yelp
PDF
Apache DeltaSpike the CDI toolbox
PDF
Apache DeltaSpike: The CDI Toolbox
A data driven etl test framework sqlsat madison
Agile db testing_techniques
Test Driven Development with Sql Server
Database development unit test with tSQLt
Test Driven Development with JavaFX
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
Unit Testing SQL Server
Just Do It! ColdBox Integration Testing
Get Testing with tSQLt - SQL In The City Workshop 2014
Qt test framework
 
POUG2019 - Test your PL/SQL - your database will love you
Introduction to SoapUI day 2
Bgoug 2019.11 test your pl sql - not your patience
1.Python_Testing_Using_PyUnit_Pytest.pptx
2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx
How to integrate_custom_openstack_services_with_devstack
QuerySurge for DevOps
PaaSTA: Running applications at Yelp
Apache DeltaSpike the CDI toolbox
Apache DeltaSpike: The CDI Toolbox
Ad

More from Eduardo Piairo (20)

PDF
ADDO 2021: Why and how to include database changes in the deployment pipeline
PDF
Start by changing the way of work
PDF
Operations for databases – The DevOps journey
PDF
Deployment pipeline for databases
PDF
Deployment pipeline for databases
PDF
Operations for databases: the agile/devops journey
PDF
Setting a WordPress website using Azure Web Apps And Azure Database for MySQL
PDF
SQL Server 2017 CLR
PDF
SQL Server 2017 CLR
PDF
Deployment pipeline for Azure SQL Databases
PDF
Operations for databases – the agile/devops journey
PDF
Delivering changes for applications and databases @ Azure
PDF
Delivering changes for databases and applications
PDF
Workshop: Delivering chnages for applications and databases
PDF
Database Source Control: Migrations vs State
PDF
Road to database automation: database source control
PDF
Deployment Pipeline for databases (Azure SQL Database, SQL Server)
PPTX
Database Source Control: Migrations vs State
PDF
Delivering changes for applications and databases
PDF
DbOps, DevOps and Ops
ADDO 2021: Why and how to include database changes in the deployment pipeline
Start by changing the way of work
Operations for databases – The DevOps journey
Deployment pipeline for databases
Deployment pipeline for databases
Operations for databases: the agile/devops journey
Setting a WordPress website using Azure Web Apps And Azure Database for MySQL
SQL Server 2017 CLR
SQL Server 2017 CLR
Deployment pipeline for Azure SQL Databases
Operations for databases – the agile/devops journey
Delivering changes for applications and databases @ Azure
Delivering changes for databases and applications
Workshop: Delivering chnages for applications and databases
Database Source Control: Migrations vs State
Road to database automation: database source control
Deployment Pipeline for databases (Azure SQL Database, SQL Server)
Database Source Control: Migrations vs State
Delivering changes for applications and databases
DbOps, DevOps and Ops

Recently uploaded (20)

PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
ai tools demonstartion for schools and inter college
PPTX
Mini project ppt template for panimalar Engineering college
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PPT
Introduction Database Management System for Course Database
PDF
5 Lead Qualification Frameworks Every Sales Team Should Use
PPTX
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
PPTX
Safe Confined Space Entry Monitoring_ Singapore Experts.pptx
PDF
top salesforce developer skills in 2025.pdf
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Digital Strategies for Manufacturing Companies
PPTX
Transform Your Business with a Software ERP System
How Creative Agencies Leverage Project Management Software.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
ai tools demonstartion for schools and inter college
Mini project ppt template for panimalar Engineering college
How to Migrate SBCGlobal Email to Yahoo Easily
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
A REACT POMODORO TIMER WEB APPLICATION.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Materi_Pemrograman_Komputer-Looping.pptx
PTS Company Brochure 2025 (1).pdf.......
Introduction Database Management System for Course Database
5 Lead Qualification Frameworks Every Sales Team Should Use
FLIGHT TICKET RESERVATION SYSTEM | FLIGHT BOOKING ENGINE API
Safe Confined Space Entry Monitoring_ Singapore Experts.pptx
top salesforce developer skills in 2025.pdf
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
The Five Best AI Cover Tools in 2025.docx
ManageIQ - Sprint 268 Review - Slide Deck
Digital Strategies for Manufacturing Companies
Transform Your Business with a Software ERP System

Adding unit tests with tSQLt to the database deployment pipeline

  • 1. ADDING UNIT TESTS WITH TSQLT TO THE DATABASE DEPLOYMENT PIPELINE Eduardo Piairo @EdPiairo #TugaIT
  • 2. ABOUT ME Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT @EdPiairo https://pt.linkedin.com/in/jesuspiairo [email protected] http://www.eduardopiairo.com/ Eduardo Piairo DevOps Coach @ Natixis DevOps Porto Founder Friend of Redgate
  • 3. TSQLT Adding 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, #TugaIT
  • 4. TSQLT INSTALL Adding 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, #TugaIT
  • 5. 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, #TugaIT
  • 6. 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, #TugaIT
  • 7. TSQLT CREATE Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT EXEC tSQLt.NewTestClass my_new_test_class’; GO CREATE PROCEDURE my_new_test_class.[test something imortant] 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;
  • 8. TSQLT RUN Adding 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, #TugaIT
  • 9. 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, #TugaIT
  • 10. 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 @EdPiairo, #TugaIT
  • 11. ISOLATING OBJECTS Adding 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, #TugaIT
  • 12. ISOLATING OBJECTS Adding unit tests with tSQLt to the database deployment pipeline • ApplyConstraint • RemoveObjectIfExists • ApplyTrigger • RemoveObject @EdPiairo, #TugaIT
  • 13. ASSERTS Adding 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, #TugaIT
  • 14. SQLTEST Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT • Redgate SQL Test • Add-in for SSMS for creating and running unit tests • Measure the code coverage of those tests
  • 15. ADDING TSQLT TO THE DEPLOYMENT PIPEPLINE Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT Source Control Continuous Integration Continuous Delivery
  • 16. ADDING TSQLT TO THE DEPLOYMENT PIPEPLINE Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT Source Control Continuous Integration Continuous Delivery
  • 17. SOURCE CONTROL Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT • GitHub • T-SQL migrations • tSQLt tests • Pester tests • Building scripts
  • 18. CONTINUOUS INTEGRATION Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT • VSTS • TEST environment setup (Local Machine | Docker) • Test and Report • Build artefact
  • 19. CONTINUOUS DELIVERY Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo, #TugaIT • Octopus Deploy • Local Machine • Azure
  • 20. REFERENCES Adding unit tests with tSQLt to the database deployment pipeline • https://tsqlt.org/user-guide/ • https://courses.agilesql.club/ @EdPiairo, #TugaIT
  • 21. Q&A Adding unit tests with tSQLt to the database deployment pipeline @EdPiairo https://pt.linkedin.com/in/jesuspiairo [email protected] http://www.eduardopiairo.com/