0% found this document useful (0 votes)
181 views

Andrew Jones - TSQLT Tests Unit Test Your SQL Server Code and Sleep Better

The document discusses unit testing SQL Server code using tSQLt, a framework for writing and running unit tests for Transact-SQL code. It highlights benefits like robust code, automated testing to find bugs early, and improved reputation. The document demonstrates how to install tSQLt, write a basic unit test isolating code using fakes and spies, and run tests. It also discusses test-driven development, performance testing, code coverage, and standards compliance tools that integrate with tSQLt.

Uploaded by

anon_660430481
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views

Andrew Jones - TSQLT Tests Unit Test Your SQL Server Code and Sleep Better

The document discusses unit testing SQL Server code using tSQLt, a framework for writing and running unit tests for Transact-SQL code. It highlights benefits like robust code, automated testing to find bugs early, and improved reputation. The document demonstrates how to install tSQLt, write a basic unit test isolating code using fakes and spies, and run tests. It also discusses test-driven development, performance testing, code coverage, and standards compliance tools that integrate with tSQLt.

Uploaded by

anon_660430481
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

tSQLt Tests:

Unit Test Your SQL


Server Code and
Sleep Better
Andy Jones
@SQLBits
#tSQLt
Why?
• Robust Code
• Automate
• Save Money
• Improve Reputation
• Senior Position
• Enjoyable
• DevOps
Agenda

Unit Testing

Test Driven Development

tSQLt

Faking and Isolation

Third Party Tools


“DevOps is the
union of people,
process, and
products to
enable
continuous
delivery of value
to our end users”
Sam
DevOps Guckenheimer
DevOps

11.7 seconds

50 per day

Thousands per day

https://techbeacon.com/10-companies-killing-it-devops
Testing?

Senior developer:
“How do you test
your code?”
Junior developer:
“I compile it”
Database Release Spectrum
Typical Process

Sprint Build Test Rework Test Live


Could not find stored procedure 'dbo.InsertOrder'

Invalid column name 'TeamMember'

Procedure or function 'CustomerSelect' expects parameter '@Active', which was not supplied

Operand type clash: datetime2 is incompatible with int

The INSERT statement conflicted with the FOREIGN KEY constraint


Blame

"It's not my
machine it's your
code"
"It's not my code
it's your machine"
Unit Testing
• Same Language
• Smallest Unit
• Isolated
• Any Order
• Repeatable
• Each Test Every Time
• Automated
• Remove Human Error
• Familiar to Devs JUnit | xUnit | Pester
Test Driven Development

Write All Tests All Tests


Test Fails Code Refactor
Test Pass Pass
Measure Twice Cut Once
tSQLt
• Open source on GitHub
• Free
• 2005 and up
• Framework and tests are TSQL
• Independent via transactions
• Methods to fake objects and isolate
Install

http://tsqlt.org/downloads
Files
Demo - Install
Security

Enable CLR
SetClrEnabled.sql

EXECUTE sp_configure 'clr enabled', 1;


RECONFIGURE;
GO
Star Schema
Demo – Create Our First Test
Test Structure

Assemble Act Assert


Assemble

Fake
Fake Table
Function

Spy
Fake View
Procedure
Act

--Act: Call the Get_Unknown_Key function


SELECT @Actual = dbo.Get_Unknown_Key();
Assert

AssertEmptyTable
AssertEquals
AssertEqualsString
AssertEqualsTable
AssertEqualsTableSchema
AssertLike
AssertNotEquals
AssertObjectDoesNotExist
AssertObjectExists
AssertResultSetsHaveSameMetaData
Fail
Running Tests

Single Class

All
Prod Bugs Cost You 100x

100

10
1 1 5

Reqs Design Dev Test Prod


Source: http://www.riceconsulting.com/public_pdf/STBC-WM.pdf
Demo – Isolation
Test Heuristics

Some
One
Zero
Infographic

http://sqlity.net/lp-heuristics
Demo – Static Data
Clean Up

Not for production 

EXECUTE tSQLt.Uninstall;
Demo – Spy Procedure
Performance Testing? Functional Only
Modular
Optimiser Help
Which SP Are Tests

Starts
Test
Extended
Property ✓
Naming Convention

EXECUTE tSQLt.NewTestClass
@ClassName = 'Orders';

CREATE PROCEDURE Orders.[test Failed Order]


AS

--assemble
--act
--assert
RETURN;
Extended Property
tSQLt.TestClasses

CREATE VIEW tSQLt.TestClasses


AS
SELECT s.name AS Name, s.schema_id AS SchemaId
FROM sys.extended_properties ep
JOIN sys.schemas s
ON ep.major_id = s.schema_id
WHERE ep.name = N'tSQLt.TestClass';
tSQLt.Tests

CREATE VIEW tSQLt.Tests


AS
SELECT classes.SchemaId, classes.Name AS TestClassName,
procs.object_id AS ObjectId, procs.name AS Name
FROM tSQLt.TestClasses classes
JOIN sys.procedures procs ON classes.SchemaId = procs.schema_id
WHERE LOWER(procs.name) LIKE 'test%';
Demo – Setup Procedure
Third Party Tools

Redgate Devart ApexSQL


SQL Cop http://sqlcop.lessthandot.com
SQL Cop

Missing Compat Column


Foreign Level data types
Keys (Numeric
vs. Int)
Standards
Code Coverage
“Testing can be used to
show the presence of
bugs, but never to show
their absence”
Edsger W. Dijkstra
Summary
• Robust
• Find bugs early
• Automated
• Shared
• Repeatable
• Better you
Example Database
Call To Action
“The best time to plant
a tree is twenty years
ago. The second best
time is now”
Chinese Proverb
Just like Jimi Hendrix …
We love to get feedback

Please complete the session feedback


forms
SQLBits - It's all about the
community...

Please visit Community Corner, we are trying


this year to get more people to learn about the
SQL Community, equally if you would be happy
to visit the community corner we’d really
appreciate it.
Thanks

Andy Jones [email protected] @SQLSprint

You might also like