SlideShare a Scribd company logo
T-SQL	Unit	Testing?	
Agenda	
This	Is	Ridiculous:	The	Challenges	of	DB	Unit	Testing	
There	Is	Hope:	The	tSQLt	Approach	
The	Big	Picture	
Digging	Deeper	
Behind	The	Wall	
Isolating	Dependencies	
Assertions	&	Expectations	
What	Can	Go	Wrong	
Limitations	
Show	Time	
That	Was	Great:	Q&A	
Further	Reading	
It Can Be Done!
This	Is	Ridiculous	
	
Why	databases	are	hard	to	test:	
	
Too	much	work	to	create	test	schemas	and	data	
Too	much	work	to	arrange	test	scenarios	&	cleanup	
SQL	is	not	Object	Oriented	(No	Polymorphism)	
How	do	you	create	mocks,	fakes	or	spies?	
Too	much	work	to	make	assertions	
Inconvenient	switching	between	tools
There	is	Hope!
tSQLt is a free, open source database unit
testing framework for Microsoft SQL Server:
• Unit Tests are written in T-SQL
• Tests automatically run within transactions – this keeps tests
independent and reduces any cleanup work you need
• Tests can be grouped together within a schema – allowing you to
organize your tests and use common setup methods
• Output can be generated in plain text or XML – making it easier to
integrate with a continuous integration tool
• Provides the ability to fake tables and views, and to create stored
procedure spies – allowing you to isolate the code which you are
testing
The	Big	Picture	
EXEC tSQLt.NewTestClass 'testGroupName'
GO
CREATE PROCEDURE testGroupName.SetUp
AS
BEGIN …
END
GO
CREATE PROCEDURE testGroupName.[Test 01 - Description]
AS
BEGIN …
END
GO
CREATE PROCEDURE testGroupName.[Test 02 - Description]
AS
BEGIN …
END
GO
…
EXEC tSQLt.Run 'testGroupName'
The fixture Set Up is a stored
procedure that runs before each test
and it is useful to set up common
data and scenarios between tests
Unit Tests are Stored Procedures.
Each test runs as an independent
transaction.
Runs all the tests in the class.
To run all the tests in the database
you can use:
tSQLt.RunAll
The class declaration is
used to defined a
schema for a group of
related tests meant to
run together
Digging	Deeper	
The basic flow of a tSQLt unit test looks and
feels just like a regular OOP unit test
	Arrange
Act
Assert
Behind	The	Wall	
Every time we run test, under the hood tSQLt
performs the following steps:
Start a new transaction X
Run the stored procedure named “Setup”
if present within the class (schema) of the test
Run the test stored procedure
Rollback transaction X
This ensures isolation and saves time for setup and cleanup!
Isolating	
Dependencies		
Isolate data and contraints:
tSQLt.FakeTable
@TableName
Creates a copy of the original table without contraints
Isolate from stored procedure and function dependencies:
tSQLt.SpyProcedure
@ProcedureName denotes the stored procedure to isolate from
@CommandToExecute will be executed in place of the stored procedure
Table created to log when isolated procedure is called
@ProcedureName_SpyProcedureLog - Records the parameters passed
tSQLt.FakeFunction
@FunctionName denotes the function to isolate from
@FakeFunctionName will be executed in place of the original function
Assertions &
Expectations
tSQLt.AssertEqualsTable
@Expected denotes a table with the expected results
@Actual denotes a table with the actual results
tSQLt.AssertResultSetsHaveSameMetaData
@Expected denotes a table with the expected metadata
@Actual denotes a table with the actual metadata
tSQLt.AssertEmptyTable
@TableName expected to be empty
tSQLt.AssertEquals / tSQLt.AssertNotEquals
@Expected value
@Actual value
tSQLt.ExpectException / tSQLt. ExpectNoException
Used to verify raised exception or try/catch blocks
What Can
Go Wrong
Database ownership issue
T-SQL current date and time functions
Some data types are not supported in assertions
(E.g. text, blob)
Functions and Views with Schemabinding
Insert Into Exec
Limitations
No time measurement
No inconclusive status for tests
Only one setup routine can be written per schema
No way to disable a test temporarily
No “SpyFunction” tool
No straight cross database support
Empty tests pass by default
No Data driven tests
No free test plugin for SQL Server Management Studio
Show Time
I will now demonstrate how to:
Install tSQLt
Unit test a function
Unit test a stored procedure with dependencies
That Was Great!
Questions?
Comments?
Feedback?
Here Are My Contacts:
Email
giovanni.scerra@afsi.com
LinkedIn
http://www.linkedin.com/pub/giovanni-scerra/10/940/348
Further
Reading
tSQLt Tutorial
http://tsqlt.org/user-guide/tsqlt-tutorial/
Ten Things I Wish I’d Known When I Started Using tSQLt and SQL Test
https://www.simple-talk.com/sql/sql-tools/ten-things-i-wish-i%E2%80%99d-known-when-i-started-using-tsqlt-and-sql-test/
Test Driven Database Development with tSQLt
http://datacentricity.net/tsqlt/
Video Training (req. subscription): Unit Testing T-SQL Code with tSQLt
http://pluralsight.com/training/Courses/TableOfContents/unit-testing-t-sql-tsqlt
…and keep an eye on the upcoming book:
Database Unit Testing for SQL Server Using tSQLt
http://www.amazon.com/Database-Testing-Server-Robert-Martin/dp/013356432

More Related Content

What's hot (20)

PPTX
Query Optimization in SQL Server
Rajesh Gunasundaram
 
PDF
Uncovering SQL Server query problems with execution plans - Tony Davis
Red Gate Software
 
PDF
[EPPG] Oracle to PostgreSQL, Challenges to Opportunity
Equnix Business Solutions
 
PDF
Avoid boring work_v2
Marcin Przepiórowski
 
PDF
Why & how to optimize sql server for performance from design to query
Antonios Chatzipavlis
 
PPTX
Soap UI - Lesson3
Qualitest
 
PDF
QuerySurge for DevOps
RTTS
 
PPTX
Ultimate Free SQL Server Toolkit
Kevin Kline
 
PPTX
Soap UI - Lesson2
Qualitest
 
PDF
Database and application performance vivek sharma
aioughydchapter
 
PDF
An Approach to Sql tuning - Part 1
Navneet Upneja
 
PPTX
Oracle Performance Tuning Training | Oracle Performance Tuning
OracleTrainings
 
PPTX
Incredible ODI tips to work with Hyperion tools that you ever wanted to know
Rodrigo Radtke de Souza
 
PDF
SQL Server Tuning to Improve Database Performance
Mark Ginnebaugh
 
PPTX
Oracle Database Performance Tuning Basics
nitin anjankar
 
PPTX
Getting to know oracle database objects iot, mviews, clusters and more…
Aaron Shilo
 
PDF
Agile db testing_techniques
Tarik Essawi
 
PPT
Advanced t sql - querying and programming inside sql server
Vinod Kumar
 
PPTX
An introduction to QuerySurge webinar
RTTS
 
PPTX
SQL Server Query Optimization, Execution and Debugging Query Performance
Vinod Kumar
 
Query Optimization in SQL Server
Rajesh Gunasundaram
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Red Gate Software
 
[EPPG] Oracle to PostgreSQL, Challenges to Opportunity
Equnix Business Solutions
 
Avoid boring work_v2
Marcin Przepiórowski
 
Why & how to optimize sql server for performance from design to query
Antonios Chatzipavlis
 
Soap UI - Lesson3
Qualitest
 
QuerySurge for DevOps
RTTS
 
Ultimate Free SQL Server Toolkit
Kevin Kline
 
Soap UI - Lesson2
Qualitest
 
Database and application performance vivek sharma
aioughydchapter
 
An Approach to Sql tuning - Part 1
Navneet Upneja
 
Oracle Performance Tuning Training | Oracle Performance Tuning
OracleTrainings
 
Incredible ODI tips to work with Hyperion tools that you ever wanted to know
Rodrigo Radtke de Souza
 
SQL Server Tuning to Improve Database Performance
Mark Ginnebaugh
 
Oracle Database Performance Tuning Basics
nitin anjankar
 
Getting to know oracle database objects iot, mviews, clusters and more…
Aaron Shilo
 
Agile db testing_techniques
Tarik Essawi
 
Advanced t sql - querying and programming inside sql server
Vinod Kumar
 
An introduction to QuerySurge webinar
RTTS
 
SQL Server Query Optimization, Execution and Debugging Query Performance
Vinod Kumar
 

Viewers also liked (16)

PDF
A data driven etl test framework sqlsat madison
Terry Bunio
 
PDF
Microsoft SQL Server Testing Frameworks
Mark Ginnebaugh
 
PPTX
Automated Testing with Databases
Stephen Ritchie
 
PDF
Unit testing 101
Erkin Ünlü
 
PDF
How to improve your unit tests?
Péter Módos
 
PPTX
Database Unit Testing Made Easy with VSTS
Sanil Mhatre
 
PDF
Efficient JavaScript Unit Testing, JavaOne China 2013
Hazem Saleh
 
PDF
Unit testing JavaScript using Mocha and Node
Josh Mock
 
PDF
Présidentielle 2017 : les parrainages en Seine-et-Marne
LaRep77
 
PPTX
An Introduction to Unit Testing
Joe Tremblay
 
PPTX
JUnit- A Unit Testing Framework
Onkar Deshpande
 
PDF
Unit testing of spark applications
Knoldus Inc.
 
PPTX
Introduction to White box testing
Aliaa Monier Ismaail
 
PPTX
UNIT TESTING PPT
suhasreddy1
 
PPT
Software Testing
Mousmi Pawar
 
PPTX
What is a Data Warehouse and How Do I Test It?
RTTS
 
A data driven etl test framework sqlsat madison
Terry Bunio
 
Microsoft SQL Server Testing Frameworks
Mark Ginnebaugh
 
Automated Testing with Databases
Stephen Ritchie
 
Unit testing 101
Erkin Ünlü
 
How to improve your unit tests?
Péter Módos
 
Database Unit Testing Made Easy with VSTS
Sanil Mhatre
 
Efficient JavaScript Unit Testing, JavaOne China 2013
Hazem Saleh
 
Unit testing JavaScript using Mocha and Node
Josh Mock
 
Présidentielle 2017 : les parrainages en Seine-et-Marne
LaRep77
 
An Introduction to Unit Testing
Joe Tremblay
 
JUnit- A Unit Testing Framework
Onkar Deshpande
 
Unit testing of spark applications
Knoldus Inc.
 
Introduction to White box testing
Aliaa Monier Ismaail
 
UNIT TESTING PPT
suhasreddy1
 
Software Testing
Mousmi Pawar
 
What is a Data Warehouse and How Do I Test It?
RTTS
 
Ad

Similar to Unit Testing SQL Server (20)

PDF
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
DOCX
Sq lite
Revuru Bharadwaja
 
PDF
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
PDF
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
PDF
Adding unit tests to the database deployment pipeline
Eduardo Piairo
 
PPT
Myth busters - performance tuning 102 2008
paulguerin
 
PDF
Adding unit tests to the database deployment pipeline
Eduardo Piairo
 
DOCX
Database testing
Pesara Swamy
 
PPT
A testing framework for Microsoft SQL-Server
elliando dias
 
ODP
Performance tuning
ami111
 
PPTX
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
PDF
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
Sperasoft
 
PDF
SELJE_Database_Unit_Testing_Slides.pdf
Eric Selje
 
PPT
Optimizing Data Accessin Sq Lserver2005
rainynovember12
 
PDF
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
Amanda Lam
 
PDF
Database development unit test with tSQLt
Sergio Govoni
 
PPTX
Data Movement issues: Explicit SQL Pass-Through can do the trick
Kiran Venna
 
PPTX
TSQL in SQL Server 2012
Eduardo Castro
 
PPTX
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
Adding unit tests to the database deployment pipeline
Eduardo Piairo
 
Myth busters - performance tuning 102 2008
paulguerin
 
Adding unit tests to the database deployment pipeline
Eduardo Piairo
 
Database testing
Pesara Swamy
 
A testing framework for Microsoft SQL-Server
elliando dias
 
Performance tuning
ami111
 
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
Sperasoft
 
SELJE_Database_Unit_Testing_Slides.pdf
Eric Selje
 
Optimizing Data Accessin Sq Lserver2005
rainynovember12
 
Waiting too long for Excel's VLOOKUP? Use SQLite for simple data analysis!
Amanda Lam
 
Database development unit test with tSQLt
Sergio Govoni
 
Data Movement issues: Explicit SQL Pass-Through can do the trick
Kiran Venna
 
TSQL in SQL Server 2012
Eduardo Castro
 
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Ad

More from Giovanni Scerra ☃ (6)

PDF
Dependency Injection
Giovanni Scerra ☃
 
PDF
Getting Started With QA Automation
Giovanni Scerra ☃
 
PPT
Thoughtful Software Design
Giovanni Scerra ☃
 
PPTX
Improving Estimates
Giovanni Scerra ☃
 
PDF
Getting Started With Testing
Giovanni Scerra ☃
 
PDF
Say It With Javascript
Giovanni Scerra ☃
 
Dependency Injection
Giovanni Scerra ☃
 
Getting Started With QA Automation
Giovanni Scerra ☃
 
Thoughtful Software Design
Giovanni Scerra ☃
 
Improving Estimates
Giovanni Scerra ☃
 
Getting Started With Testing
Giovanni Scerra ☃
 
Say It With Javascript
Giovanni Scerra ☃
 

Recently uploaded (20)

PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 

Unit Testing SQL Server