+ All Categories
Home > Documents > Unit Testing SQL Server

Unit Testing SQL Server

Date post: 17-Jul-2015
Category:
Upload: giovanni-scerra
View: 90 times
Download: 0 times
Share this document with a friend
Popular Tags:
13
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 ThatWasGreat:Q&A Further Reading It Can Be Done!
Transcript
Page 1: Unit Testing SQL Server

T-SQLUnitTesting?

Agenda

� ThisIsRidiculous:TheChallengesofDBUnitTesting

� ThereIsHope:ThetSQLtApproach

� TheBigPicture

� DiggingDeeper

� BehindTheWall

� IsolatingDependencies

� Assertions&Expectations

� WhatCanGoWrong

� Limitations

� ShowTime

� ThatWasGreat:Q&A

� FurtherReading

It Can Be Done!

Page 2: Unit Testing SQL Server

ThisIsRidiculous

Whydatabasesarehardtotest:

� Toomuchworktocreatetestschemasanddata

� Toomuchworktoarrangetestscenarios&cleanup

� SQLisnotObjectOriented(NoPolymorphism)

� Howdoyoucreatemocks,fakesorspies?

� Toomuchworktomakeassertions

� Inconvenientswitchingbetweentools

Page 3: Unit Testing SQL Server

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

Page 4: Unit Testing SQL Server

TheBigPicture

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

Page 5: Unit Testing SQL Server

DiggingDeeper

The basic flow of a tSQLt unit test looks and feels just like a regular OOP unit test

Arrange

Act

Assert

Page 6: Unit Testing SQL Server

BehindTheWall

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!

Page 7: Unit Testing SQL Server

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

Page 8: Unit Testing SQL Server

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

Page 9: Unit Testing SQL Server

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

Page 10: Unit Testing SQL Server

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

Page 11: Unit Testing SQL Server

Show Time

I will now demonstrate how to:

� Install tSQLt

� Unit test a function

� Unit test a stored procedure with dependencies

Page 12: Unit Testing SQL Server

That Was Great!

Questions?

Comments?

Feedback?

Here Are My Contacts: Email [email protected] LinkedIn http://www.linkedin.com/pub/giovanni-scerra/10/940/348

Page 13: Unit Testing SQL Server

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


Recommended