Testing Database Code with tSQLt

Slides:



Advertisements
Similar presentations
Test process essentials Riitta Viitamäki,
Advertisements

A comparison of MySQL And Oracle Jeremy Haubrich.
#sqlsatPordenone #sqlsat367 February 28, 2015 Testing your databases Alessandro
Design, Implementation and Maintenance
Continuous Integration for Databases Learn how to automate your build and test Steve Jones Red Gate Software Part II of the Continuous Delivery for Databases.
TESTING.
UNIT TESTING FOR SQL Prepared for SUGSA CodeLabs Alain King Paul Johnson.
Sofia, Bulgaria | 9-10 October SQL Server 2005 High Availability for developers Vladimir Tchalkov Crossroad Ltd. Vladimir Tchalkov Crossroad Ltd.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
Software Engineering 2004 Jyrki Nummenmaa 1 BACKGROUND There is no way to generally test programs exhaustively (that is, going through all execution.
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
Continuous Integration for Databases Steve Jones SQLServerCentral Red Gate Software.
INTRODUCTION TO MDX. Stick around for SWAG! (Prize drawing at 5.00 p.m)  All our volunteers and organisers do not get paid for running SPS SQLSaturday.
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.
Datazen – an overview Frank Geisler
Beginning Software Craftsmanship Brendan Enrick Steve Smith
Get testing with tSQLt Practical examples and automation Steve Jones SQLServerCentral Red Gate Software.
Upgrade on Windows 7. DownloadSoftware Download Software from link provided in Webliography: e/
Benjamin Day Real World Scrum with TFS 2015 & VSTS.
Advanced BIML topics Be a W.I.S.E. A.S.S. Me ! Self-employed BI consultant Author Trainer MCT
Continuous Integration for Databases Steve Jones SQLServerCentral Red Gate Software.
Automated Build and Test
Analysis Services in times of Continuous Integration
The What, Why and How of Unit Testing SQL Server
Securing SQL Server Processes with Certificates
Best Practices for Dynamics NAV Administration and Security
From MDS to SSRS - a short walkthrough
Data Virtualization Tutorial: Introduction to SQL Script
Making Your List and Checking It Twice
Introduction to unit and integration testing with tSQLt
Outsourcing Database Administration
Module 13: Creating Highly Concurrent SQL Server 2012 Applications
Software Testing.
Bridging the Data Science and SQL Divide for Practitioners
Do You Want To Pass In First Attempt?
DBA and IT Professional for ~9 years. Currently I am a Data Architect
Solving the Hard Problems
Automated and Repeatable Test Cases for SQL Server Development
Parameter Sniffing in SQL Server Stored Procedures
Deploying and Configuring SSIS Packages
Fun with Reporting Services Tools
From Navision Dynamics Nav to Power BI Dashboard in 45 minutes
Dimodelo Solutions.
DevOps Database Administration
Taking your application to memory
Continuous Integration For Databases
Principles of report writing
Test-Driving Your Database
DevOps Database Administration
5 WAYS TO BYPASS *OR ENSURE* SQL SERVER SECURITY MATT MARTIN
Making PowerShell Useful
Prove to your boss your database is sound - Unit Testing with tSQLt
Intro to Unit Testing with tSQLt
Testing and Test-Driven Development CSC 4700 Software Engineering
Performance tuning Tabular and PowerPivot models
Database Corruption Advanced Recovery Techniques
Please thank our sponsors!
Reliable, Repeatable, Configurable & Automated Validation with
Computer Science Testing.
DBA for ~4+years, IT Professional for 7.5 years.
PowerShell Best Practices for SQL DBA’s
Making PowerShell Useful
DAT381 Team Development with SQL Server 2005
Outsourcing Database Administration
Real World Scrum with TFS & VSTS / Azure DevOps
Your code is not just…your code
Summit Nashville /3/2019 1:48 AM
How To Load A Fact Table Really, Really Fast
THANK YOU, SPONSORS! Rockstar Sponsors!.
Your code is not just…your code
Presentation transcript:

Testing Database Code with tSQLt Frank Geisler 13.07.2013

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

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!!!

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 frank_geisler@geislers.net

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

Why unit tests? SQL Server only checks syntax not functional errors Code can be syntactical correct but it might not work as expected

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

DEMO Simple Test without tSQLt 13.07.2013 |

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

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) 13.07.2013

What is the result of the unit test? Confirms that the single parts work, does not confirm that the single parts work together! 13.07.2013

Who writes unit tests? Developers while writing code Testers increase code coverage Tests for exitsing code 13.07.2013

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 13.07.2013

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 13.07.2013

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 13.07.2013

Install tSQLt Download tsqlt.zip Enable CLR on Server (only on development server) Run tsqlt.class.sql 13.07.2013

DEMO Install tSQLt and run Example 13.07.2013 |

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“ 19.09.2018

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 19.09.2018

DEMO Using SQL Test 13.07.2013 |

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 19.09.2018

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 13.07.2013

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 12.09.2015

The Beer Database 12.09.2015

The data – View Fact.vOrders 12.09.2015

View vBeerTypes Returns all the BeerTypes in the Database 12.09.2015

Possible Tests Does the Object exist? Does it have the right output format? Are the right values returend? 12.09.2015

DEMO Writing a new test 13.07.2013 |

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 12.09.2015

DEMO Testing the output 13.07.2013 |

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 12.09.2015

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  12.09.2015

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 12.09.2015

DEMO Faking Tables 13.07.2013 |

Thank you for your attention Any questions??? 13.07.2013