SlideShare a Scribd company logo
SQLDay 2017 - Database Unit Tests with tSQLt
Database Unit Tests with tSQLt
SQLDay 2017
About the Author
Marek Maśko
• Principal Database Analyst at Sabre
• Working with SQL Server for ~7 years
• SQL DBA, Dev & Architect
• MCP since 2012
• Contact Information:
Email: marek.masko@gmail.com
LinkedIn: https://pl.linkedin.com/in/marekmasko
Twitter: @MarekMasko
SQLDay 2017
Unit Tests
• Take the smallest testable piece of
software and test it in isolated conditions.
• F.I.R.S.T Principles:
– Fast
– Isolated
– Repeatable
– Self-Validating
– Thorough and Timely
SQLDay 2017
tSQLt Framework
• Open Source Framework
• Written in TSQL and C# (CLR)
• Runs on database level
• Provides various Assert methods
• Allows object mocking
http://tsqlt.org/
https://github.com/tSQLt-org/tSQLt
SQLDay 2017
What we get?
SQLDay 2017
Requirements
• Requires CLR (SQL Server 2005 SP2 +)
• tSQLt schema
• Trustworthy database
SQLDay 2017
Test creation and execution
• tSQLt.NewTestClass
• tSQLt.Run [ [@testName = ] 'test name' ]
• tSQLt.RunAll
SQLDay 2017
tSQLt.Run
tSQLt.Run
’test class’
For each [test*] SP
Create record in
tSQLt.TestResult
Start TRANRun Setup SPRun test SP
ROLLBACK
Update record in
tSQLt.TestResult
Print results
SQLDay 2017
Assertions
• tSQLt.AssertEmptyTable
• tSQLt.AssertEquals
• tSQLt.AssertEqualsString
• tSQLt.AssertEqualsTable
• tSQLt.AssertEqualsTableSchema
• tSQLt.AssertLike
• tSQLt.AssertNotEquals
• tSQLt.AssertObjectDoesNotExist
• tSQLt.AssertObjectExists
• tSQLt.AssertResultSetsHaveSameMetaData
• tSQLt.Fail
SQLDay 2017
Isolating Dependencies
Tables:
• tSQLt.FakeTable
• tSQLt.ApplyConstraint
• tSQLt.ApplyTrigger
Procedures:
• tSQLt.SpyProcedure
Functions:
• tSQLt.FakeFunction
SQLDay 2017
Something went wrong?
[tSQLt].[Private_RenamedObjectLog] for the rescue!
SELECT OriginalName,
SCHEMA_NAME(schema_id) +'.'+ name AS [Name of Renamed Table],
create_date
FROM [tSQLt].[Private_RenamedObjectLog]
JOIN sys.objects
ON objectid = object_id
SQLDay 2017
What else?
• SqlCop by George Mastros
– http://sqlcop.lessthandot.com
– http://dl.dropbox.com/u/58229865/SQLCop%20Tests.zip
• SQLCover Code Coverage by Ed Elliott
– https://the.agilesql.club/Projects/SQLCover
– https://github.com/GoEddie/SQLCover
– https://github.com/danielpalme/ReportGenerator
SQLDay 2017
Problems
• INSERT INTO … EXEC
• Synonyms
• Schema binding
• Watch out for time
• Exceptions from rules
• Cannot run tests in parallel
SQLDay 2017
Best practices
• Separate test class for each tested object
• Single assertion per test
• Parameters testing
• Resultset schema testing
• Table schema testing (prevent changes)
THANK YOU
AND GOOD LUCK!
SQLDay 2017 - Database Unit Tests with tSQLt

More Related Content

What's hot (20)

PDF
Keep Calm And Serilog Elasticsearch Kibana on .NET Core
Maciej Szymczyk
 
PDF
Refactoring Rails applications with RubyMine
Andrzej Krzywda
 
PDF
Keep Calm And Serilog Elasticsearch Kibana on .NET Core - 132. Spotkanie WG.N...
Maciej Szymczyk
 
PPTX
Elk meetup boston - logz.io
tomerlevy9
 
PDF
Know thy cost (or where performance problems lurk)
Oren Eini
 
PDF
The Holy Grail of continuous delivery in distributed teams environment
Szymon Kurcab
 
PDF
Testing course content converted
ShreemInstituteProfe
 
PPTX
Angular4 kickstart
Foyzul Karim
 
PDF
Infra for startup
Sira Sujjinanont
 
PPTX
Entity framework core v3 from sql to no sql
Andrea Tosato
 
PDF
From Rails legacy to DDD - Pivorak, Lviv
Andrzej Krzywda
 
PDF
MongoDB World 2018: Solving Your Backup Needs Using MongoDB Ops Manager, Clou...
MongoDB
 
PDF
Async streams
Christian Nagel
 
PDF
Wongnai Engineering Story
Pattrawoot Suesatayasilp
 
PDF
Node.js server side render in the Age of APIs - Full Stack Toronto 2017
Ruy Adorno
 
PPTX
MongoDB World 2018: Using Puppet, Ansible and Ops Manager to Create Your Own ...
MongoDB
 
PPTX
Node.js Dublin Meetup April 2014
Damian Beresford
 
PDF
TabTale Architecture Overview
Assaf Gannon
 
PDF
Sustainable software development / Jätkusuutlik tarkvaraarendus
GreteStrandberg
 
PPTX
Survey of the Microsoft Azure Data Landscape
Ike Ellis
 
Keep Calm And Serilog Elasticsearch Kibana on .NET Core
Maciej Szymczyk
 
Refactoring Rails applications with RubyMine
Andrzej Krzywda
 
Keep Calm And Serilog Elasticsearch Kibana on .NET Core - 132. Spotkanie WG.N...
Maciej Szymczyk
 
Elk meetup boston - logz.io
tomerlevy9
 
Know thy cost (or where performance problems lurk)
Oren Eini
 
The Holy Grail of continuous delivery in distributed teams environment
Szymon Kurcab
 
Testing course content converted
ShreemInstituteProfe
 
Angular4 kickstart
Foyzul Karim
 
Infra for startup
Sira Sujjinanont
 
Entity framework core v3 from sql to no sql
Andrea Tosato
 
From Rails legacy to DDD - Pivorak, Lviv
Andrzej Krzywda
 
MongoDB World 2018: Solving Your Backup Needs Using MongoDB Ops Manager, Clou...
MongoDB
 
Async streams
Christian Nagel
 
Wongnai Engineering Story
Pattrawoot Suesatayasilp
 
Node.js server side render in the Age of APIs - Full Stack Toronto 2017
Ruy Adorno
 
MongoDB World 2018: Using Puppet, Ansible and Ops Manager to Create Your Own ...
MongoDB
 
Node.js Dublin Meetup April 2014
Damian Beresford
 
TabTale Architecture Overview
Assaf Gannon
 
Sustainable software development / Jätkusuutlik tarkvaraarendus
GreteStrandberg
 
Survey of the Microsoft Azure Data Landscape
Ike Ellis
 

Similar to SQLDay 2017 - Database Unit Tests with tSQLt (20)

PDF
Database development unit test with tSQLt
Sergio Govoni
 
PPT
A testing framework for Microsoft SQL-Server
elliando dias
 
PDF
SELJE_Database_Unit_Testing_Slides.pdf
Eric Selje
 
PDF
Test Driven Development with Sql Server
David P. Moore
 
PPTX
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
Alessandro Alpi
 
PDF
Unit Testing SQL Server
Giovanni Scerra ☃
 
PPTX
Get Testing with tSQLt - SQL In The City Workshop 2014
Red Gate Software
 
PDF
A data driven etl test framework sqlsat madison
Terry Bunio
 
PPTX
Evaluating Test Driven Development And Parameterized Unit Testing In Dot Net ...
mdfachowdhury
 
PDF
POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQL
Jacek Gebal
 
PPT
Building a Testable Data Access Layer
Todd Anglin
 
PPTX
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Codecamp Romania
 
PPTX
tSQLt Database Unit Testing
Dexter Baga
 
PDF
Adding unit tests to the database deployment pipeline
Eduardo Piairo
 
PDF
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
PPT
xUnit Style Database Testing
Chris Oldwood
 
PDF
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
Jacek Gebal
 
PDF
utplsql.pdf
vijayv991893
 
PPT
Automated Testing with Databases
elliando dias
 
PPTX
Database Unit Testing Made Easy with VSTS
Sanil Mhatre
 
Database development unit test with tSQLt
Sergio Govoni
 
A testing framework for Microsoft SQL-Server
elliando dias
 
SELJE_Database_Unit_Testing_Slides.pdf
Eric Selje
 
Test Driven Development with Sql Server
David P. Moore
 
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
Alessandro Alpi
 
Unit Testing SQL Server
Giovanni Scerra ☃
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Red Gate Software
 
A data driven etl test framework sqlsat madison
Terry Bunio
 
Evaluating Test Driven Development And Parameterized Unit Testing In Dot Net ...
mdfachowdhury
 
POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQL
Jacek Gebal
 
Building a Testable Data Access Layer
Todd Anglin
 
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Codecamp Romania
 
tSQLt Database Unit Testing
Dexter Baga
 
Adding unit tests to the database deployment pipeline
Eduardo Piairo
 
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
xUnit Style Database Testing
Chris Oldwood
 
prohuddle-utPLSQL v3 - Ultimate unit testing framework for Oracle
Jacek Gebal
 
utplsql.pdf
vijayv991893
 
Automated Testing with Databases
elliando dias
 
Database Unit Testing Made Easy with VSTS
Sanil Mhatre
 
Ad

More from Marek Maśko (7)

PDF
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
Marek Maśko
 
PDF
SQL Operations Studio - new multi-platform tool for SQL Server database devel...
Marek Maśko
 
PDF
SQLSaturday 664 - Troubleshoot SQL Server performance problems like a Microso...
Marek Maśko
 
PDF
SQL Server - Querying and Managing XML Data
Marek Maśko
 
PDF
SQL Server - Using Tools to Analyze Query Performance
Marek Maśko
 
PDF
DevOps and databases
Marek Maśko
 
PDF
PLSSUG - Troubleshoot SQL Server performance problems like a Microsoft Engineer
Marek Maśko
 
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
Marek Maśko
 
SQL Operations Studio - new multi-platform tool for SQL Server database devel...
Marek Maśko
 
SQLSaturday 664 - Troubleshoot SQL Server performance problems like a Microso...
Marek Maśko
 
SQL Server - Querying and Managing XML Data
Marek Maśko
 
SQL Server - Using Tools to Analyze Query Performance
Marek Maśko
 
DevOps and databases
Marek Maśko
 
PLSSUG - Troubleshoot SQL Server performance problems like a Microsoft Engineer
Marek Maśko
 
Ad

Recently uploaded (20)

PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PDF
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
PPTX
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
PDF
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PPT
AI Future trends and opportunities_oct7v1.ppt
SHIKHAKMEHTA
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PDF
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
PPTX
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
Research Methodology Overview Introduction
ayeshagul29594
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
AI Future trends and opportunities_oct7v1.ppt
SHIKHAKMEHTA
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 

SQLDay 2017 - Database Unit Tests with tSQLt

  • 2. Database Unit Tests with tSQLt
  • 3. SQLDay 2017 About the Author Marek Maśko • Principal Database Analyst at Sabre • Working with SQL Server for ~7 years • SQL DBA, Dev & Architect • MCP since 2012 • Contact Information: Email: [email protected] LinkedIn: https://pl.linkedin.com/in/marekmasko Twitter: @MarekMasko
  • 4. SQLDay 2017 Unit Tests • Take the smallest testable piece of software and test it in isolated conditions. • F.I.R.S.T Principles: – Fast – Isolated – Repeatable – Self-Validating – Thorough and Timely
  • 5. SQLDay 2017 tSQLt Framework • Open Source Framework • Written in TSQL and C# (CLR) • Runs on database level • Provides various Assert methods • Allows object mocking http://tsqlt.org/ https://github.com/tSQLt-org/tSQLt
  • 7. SQLDay 2017 Requirements • Requires CLR (SQL Server 2005 SP2 +) • tSQLt schema • Trustworthy database
  • 8. SQLDay 2017 Test creation and execution • tSQLt.NewTestClass • tSQLt.Run [ [@testName = ] 'test name' ] • tSQLt.RunAll
  • 9. SQLDay 2017 tSQLt.Run tSQLt.Run ’test class’ For each [test*] SP Create record in tSQLt.TestResult Start TRANRun Setup SPRun test SP ROLLBACK Update record in tSQLt.TestResult Print results
  • 10. SQLDay 2017 Assertions • tSQLt.AssertEmptyTable • tSQLt.AssertEquals • tSQLt.AssertEqualsString • tSQLt.AssertEqualsTable • tSQLt.AssertEqualsTableSchema • tSQLt.AssertLike • tSQLt.AssertNotEquals • tSQLt.AssertObjectDoesNotExist • tSQLt.AssertObjectExists • tSQLt.AssertResultSetsHaveSameMetaData • tSQLt.Fail
  • 11. SQLDay 2017 Isolating Dependencies Tables: • tSQLt.FakeTable • tSQLt.ApplyConstraint • tSQLt.ApplyTrigger Procedures: • tSQLt.SpyProcedure Functions: • tSQLt.FakeFunction
  • 12. SQLDay 2017 Something went wrong? [tSQLt].[Private_RenamedObjectLog] for the rescue! SELECT OriginalName, SCHEMA_NAME(schema_id) +'.'+ name AS [Name of Renamed Table], create_date FROM [tSQLt].[Private_RenamedObjectLog] JOIN sys.objects ON objectid = object_id
  • 13. SQLDay 2017 What else? • SqlCop by George Mastros – http://sqlcop.lessthandot.com – http://dl.dropbox.com/u/58229865/SQLCop%20Tests.zip • SQLCover Code Coverage by Ed Elliott – https://the.agilesql.club/Projects/SQLCover – https://github.com/GoEddie/SQLCover – https://github.com/danielpalme/ReportGenerator
  • 14. SQLDay 2017 Problems • INSERT INTO … EXEC • Synonyms • Schema binding • Watch out for time • Exceptions from rules • Cannot run tests in parallel
  • 15. SQLDay 2017 Best practices • Separate test class for each tested object • Single assertion per test • Parameters testing • Resultset schema testing • Table schema testing (prevent changes)