Download presentation
Presentation is loading. Please wait.
1
Testing Database Code with tSQLt
Frank Geisler
3
Stick around for SWAG! (Prize drawing at 5.00 p.m)
All our volunteers and organisers do not get paid for running SPS SQLSaturday Cambridge and do it because they believe in the power of community (or are just plain nuts). Please show your gratitude for making this possible by: Giving them a hug Shaking their hand Saying thank you Coming back next year Consider getting your company to pay for a precon next year Speading the word Getting involved yourself But most of all, by enjoying the day! Don’t forget to thank the sponsors for their support Thank the speakers for donating their time, energy and expenses
4
Stick around for SWAG! (Prize drawing at 5.00 p.m)
SQLCloud — XBox One!!! (make sure you know how to play the Jet Set Willy vendor competition!) SQLCloud—Dark Side Of The Moon, The Wall and Wish You Were Here Microsoft — MCP Voucher Data Idols — Supercar Driving Experience Profisee — Signed MDS Book and £50 Amazon Voucher SQLSentry — Plan Explorer PRO License Pyramid Analytics—Coffee Machine Effektor — Lego Big Ben Rencore — SPCAF Professional License (valued at $2099!) Axioworks — Amazon Fire TV and £75 Amazon Voucher DELL — Beats by Dr Dre Headphones Redgate — DLM Workshop Pluralsight — Annual Plus subscriptions All Your Base Conference — 3 x Conference Tickets VisualSP—Annual training subscription Loads of books and much much more!!!
5
Who Am I…? CEO GDS Business Intelligence GmbH
@FrankGeisler CEO GDS Business Intelligence GmbH Chapter Leader PASS Chapter Ruhrgebiet Microsoft P-TSP MVP SQL Server Author Speaker Topics: Business Intelligence, SharePoint, SQL Server Programming, Software Engineering
6
Agenda Why Unit Tests? What are Unit Tests? What is tSQLt?
How to install and run example How is tSQLt structured? SQL Test How to write a test Faking Tables
7
Why unit tests? SQL Server only checks syntax not functional errors
Code can be syntactical correct but it might not work as expected
8
What are unit tests? A simple example
Write a simple Stored Function that adds two numbers Write a test without tSQLt just to demo how unit tests work Alter the Stored Function to see how it affects the test
9
DEMO Simple Test without tSQLt |
10
So what are Unit Tests? Tests a discrete unit of code = self contained proc. or function Does not test how the proc. or functions interact with other code Isolation is important – the code that shall be tested should not be affected by other things outside the code Test should be repeatable not only meet requiremets once Good for automation
11
How to write Unit Tests? Build a selection of tests
different sets of input parameters Max / Min values for parameters Wrong parameters (to determine how the code will act on them) IMPORTANT: Test only one question at a time Series of discrete tests Each test reflects a requirement to our code Each test can be tied back to the specification Unit tests only part of the testing (other test necessary like integration test)
12
What is the result of the unit test?
Confirms that the single parts work, does not confirm that the single parts work together!
13
Who writes unit tests? Developers while writing code Testers
increase code coverage Tests for exitsing code
14
When write unit tests? After development During development
For existing code Before refactioring existing production code Cover instances where bugs have been discovered During development Before development Think about code structure Focused development – clear line for requirements Extreme Programming / Agile Methods
15
Pros of unit tests Certainty that codes works as expected
Code meets requirements Enforces good design practice by having to isolate functionality More of the design is done in advance Simple error checking Unit Tests show which Module fails Error is either in unit or in test
16
What is tSQLt? tSQLt is Free! (www.tsqlt.org) Based on T-SQL
Tests are Stored Procedures Smaller than a testing framework outside SQL Server Can mock objects (like tables) to isolate Code Utilize power of transactions (rollback changes) No (potentially) buggy tear down script Uses CLR does not run on Azure
17
Install tSQLt Download tsqlt.zip
Enable CLR on Server (only on development server) Run tsqlt.class.sql
18
DEMO Install tSQLt and run Example |
19
How is tSQLt Structured in the Database
Schema tSQLt – all the Stored Procedures to setup and run tests „Test-Classes“: in tSQLt they are Schemas Tests: Stored Procedure within a test class – must start with the word „test“
20
SQL Test Commercial Product from redgate Nice GUI for tSQLt
Not necessary for using tSQLt (but makes using tSQLt a lot nicer) Lower barrier to use tSQLt Adoption might be higher Can install tSQLt to your database If you want the newest version of tSQLt install it manually
21
DEMO Using SQL Test |
22
How to write a test? Set the scene – isolate test from outside dependencies Define what will be a sucessful test Run the object under test and capture output Compare output of object to expectation
23
What the tSQLt Framework does…
Start a transaction for the test Run the setup procedure (if any) Run the test (as defined on the slide before this) Set the scene – isolate test from outside dependencies Define what will be a sucessful test Run the object under test and capture output Compare output of object to expectation Rollback transaction
24
Writing new tests Often Test is written before code is written
By default tSQLt assumes that the test passes DANGEROUS: If a test is not implemented the false impression is raised that everything is OK Empty Tests pass! => Important to avoid false positives
25
The Beer Database
26
The data – View Fact.vOrders
27
View vBeerTypes Returns all the BeerTypes in the Database
28
Possible Tests Does the Object exist?
Does it have the right output format? Are the right values returend?
29
DEMO Writing a new test |
30
Testing the output Data table to be returned Expected set of data
Capture output of object under test Assert that they are the same See if assertion is met
31
DEMO Testing the output |
32
Isolating from the outside
Isolate Object under test from external Influences Data Table Constraints Other Stored Procedures or Functions being called Not isolating prevents tests from being run automatically and repeatably
33
Isolating from Data Requirement of the Data
Enough to do testing but not too much Unit test shall run quickly run them frequently Use a small portion of realistic data Possible Solution: Delete Data for test and rollback Could take a while Does not isolate all dependencies Problems with Constraints or Foreign Keys Extra Effort
34
FakeTable Takes Tablename to Clone actual table Moves Objects
Fake-Table is without Data and without constraints Code under test is unchanged Minimal setup Test is repeatable
35
DEMO Faking Tables |
36
Thank you for your attention
Any questions???
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.