SlideShare a Scribd company logo
A testing framework for Microsoft SQL-Server  Jorge Almeida
Summary Database Testing in an RDBMS Unit test framework for databases Database manual testing Database automatized tests  Database testing tools TSQLUnit framework How to write T-SQL unit tests Conclusions References
Database Testing in an RDBMS
Database Testing Categories of database tests Interface tests (validate what is going into, out of, and mapped) Internal database tests (validate the internal structure, behavior and contents) Internal database tests should validate: Database methods (e.g. stored procedures, functions, and triggers) Existence of database schema elements (tables, procedures, ...) View definitions Referential integrity rules Default values for a column Data invariants for a single column Data invariants involving several columns
Database Manual Testing Combination of  PRINT  statements and ad-hoc tests with the  SQL Query Analyzer Trace/debug tools like  SQL Profiler Human judgment is required to analyze the results The tests can not easily be repeated again
Database Automatized Tests Can be repeated over and over again Improve the quality of the code (refactoring) Serve the purpose of documentation May improve the design
Database testing tools Category of database testing (interface and internal) Support the language of developing Help putting the database into a known state
Some database testing tools Data Factory Datatect DTM Data Generator Turbo Data Developers need test data against which to validate their systems. Test data generators can be particularly useful when need large amounts of data, for stress and load testing Test data generator Empirix Mercury Interactive RadView Rational Suite Test Studio Web Performance Tools simulate high usage loads on database, enabling to determine whether the system’s architecture will stand up to true production needs Testing tools for load testing DBUnit NDbUnit OUnit for Oracle  (being replaced soon by  Qute ) SQLUnit TSQLUnit Visual Studio Team Edition for Database Professionals Tools which enable regression test Unit testing tools Examples Description Category
TSQLUnit A testing framework for Microsoft SQL-Server It follows the tradition of the "xUnit" framework  TSQLUnit is open source Developed by Henrik Ekelund Unit testing for stored procedures, functions, triggers and views Available from http://sourceforge.net/projects/tsqlunit  Version 0.9 (December 15, 2002)
TSQLUnit Install Download file from  http://tsqlunit.sourceforge.net/tsqlunit_download.htm Unzip the file  Connect to database as  dbo  using the SQL Query Analyzer, execute the  tsqlunit.sql  file
Basic principles of database testing Database with good test data Shouldn’t developing against a production database  Test one feature only per test procedure Be careful of how T-SQL comparison operators work When it is difficult to write tests change the design or refactor the system
How to write T-SQL unit tests  Create a stored procedure with a name that starts with  ut  and underscore (e.g.  ut_testSomething ) Code a test, then call  tsu_failure  if test fails Execute  tsu_runTests
Test example (1) CREATE PROCEDURE  ut_CapitalizeOneSentence  AS BEGIN DECLARE  @outStr  VARCHAR (500) EXECUTE  capitalize  ‘ a string ’ , @outStr  OUTPUT IF   ASCII ( LEFT (@outStr,1)) <>  ASCII ( ‘ A ’ )  OR  @outStr  IS  NULL EXECUTE  tsu_failure  ‘ Capitalize should make the first character uppercase ’ END CREATE PROCEDURE  capitalize @inStr  VARCHAR (500)= NULL , @outStr  VARCHAR (500)= NULL   OUTPUT   AS BEGIN SET  @outStr= NULL IF  @inStr  IS  NOT NULL SET  @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
Test example (2) CREATE PROCEDURE  ut_CapitalizeNullSentence  AS BEGIN DECLARE  @outStr  VARCHAR (500) EXECUTE  capitalize  NULL , @outStr  OUTPUT IF  @outStr  IS  NOT NULL EXECUTE  tsu_failure  ‘ Capitalize should not return any value ’ END CREATE PROCEDURE  capitalize @inStr  VARCHAR (500)= NULL , @outStr  VARCHAR (500)= NULL   OUTPUT   AS BEGIN SET  @outStr= NULL IF  @inStr  IS  NOT NULL SET  @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
Test example (3) CREATE PROCEDURE  ut_CapitalizeEmptySentence  AS BEGIN DECLARE  @outStr  VARCHAR (500) EXECUTE  capitalize  ‘’ , @outStr  OUTPUT IF  @outStr<>’’ EXECUTE  tsu_failure  ‘ Capitalize should return the same value ’ END CREATE PROCEDURE  capitalize @inStr  VARCHAR (500)= NULL , @outStr  VARCHAR (500)= NULL   OUTPUT   AS BEGIN SET  @outStr= NULL IF  @inStr  IS  NOT NULL SET  @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
Test Results
Improving tests Organize tests in classes ( suites ) (e.g. rename ut_CapitalizeEmptySentence  as ut_Capitalize_EmptySentence then run the store procedure  tsu_runTest ‘Capitalize’ ) Create a unique procedure for test setup (e.g. ut_Capitalize_SetUp) Create a unique procedure for test “teardown” (e.g. ut_Capitalize_TearDown)
Conclusions Good unit testing tool for Microsoft SQL-Server stored procedures Regression test suite Some improvements needed (e.g. execution time of a single test) Be careful with error treatment in test store procedures
References http://sourceforge.net/projects/tsqlunit http://www.tassq.org http://www.agile.org/essays/databaseTesting.html http://www.ambysoft.com/books/refactoringDatabases.html http:// www.ambysoft.com/books/agileDatabaseTechniques.html
Questions ?

More Related Content

What's hot (18)

PPTX
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
PDF
IR SQLite Session #1
InfoRepos Technologies
 
PDF
SQL Readable Outputs - Oracle SQL Fundamentals
MuhammadWaheed44
 
PPTX
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
PPTX
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
PDF
SQL Functions and Operators
Mohan Kumar.R
 
PPTX
Null values, insert, delete and update in database
Hemant Suthar
 
PDF
Updat Dir
guest319770
 
PDF
Oracle SQL Fundamentals - Lecture 3
MuhammadWaheed44
 
PDF
Database Management System 1
Swapnali Pawar
 
PPTX
Database constraints
Khadija Parween
 
PPTX
Lab1 select statement
Balqees Al.Mubarak
 
PDF
Sub query_SQL
CoT
 
PPTX
Lab5 sub query
Balqees Al.Mubarak
 
PPTX
Sql operator
Pooja Dixit
 
PPT
Advanced Sql Training
bixxman
 
ODP
SQL Tunning
Dhananjay Goel
 
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
IR SQLite Session #1
InfoRepos Technologies
 
SQL Readable Outputs - Oracle SQL Fundamentals
MuhammadWaheed44
 
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
SQL Functions and Operators
Mohan Kumar.R
 
Null values, insert, delete and update in database
Hemant Suthar
 
Updat Dir
guest319770
 
Oracle SQL Fundamentals - Lecture 3
MuhammadWaheed44
 
Database Management System 1
Swapnali Pawar
 
Database constraints
Khadija Parween
 
Lab1 select statement
Balqees Al.Mubarak
 
Sub query_SQL
CoT
 
Lab5 sub query
Balqees Al.Mubarak
 
Sql operator
Pooja Dixit
 
Advanced Sql Training
bixxman
 
SQL Tunning
Dhananjay Goel
 

Similar to A testing framework for Microsoft SQL-Server (20)

PPTX
utPLSQL: Unit Testing for Oracle PL/SQL
Steven Feuerstein
 
PPT
JDBC – Java Database Connectivity
Information Technology
 
PPT
SQL Server 2000 Research Series - Transact SQL
Jerry Yang
 
PPT
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 
PDF
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
PPTX
Introduction to database
Pongsakorn U-chupala
 
PPTX
Oracle database performance tuning
Yogiji Creations
 
PPTX
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
PPT
Mxunit
VIkas Patel
 
PDF
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
PPT
Module02
Sridhar P
 
PDF
Stored procedure Notes By Durgesh Singh
imdurgesh
 
PDF
Unit Testing SQL Server
Giovanni Scerra ☃
 
PDF
04. SQL Servesdafr with CSharp WinForms.pdf
ManhHoangVan
 
PPTX
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
PPTX
Sql Server 2008 New Programmability Features
sqlserver.co.il
 
PDF
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
PPTX
Sql storeprocedure
ftz 420
 
PPTX
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
PPTX
unit 3nit unit 1_unit1_unit1_.unit 1_unit1_unit1_
townhallforme1
 
utPLSQL: Unit Testing for Oracle PL/SQL
Steven Feuerstein
 
JDBC – Java Database Connectivity
Information Technology
 
SQL Server 2000 Research Series - Transact SQL
Jerry Yang
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
Introduction to database
Pongsakorn U-chupala
 
Oracle database performance tuning
Yogiji Creations
 
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
Mxunit
VIkas Patel
 
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
Module02
Sridhar P
 
Stored procedure Notes By Durgesh Singh
imdurgesh
 
Unit Testing SQL Server
Giovanni Scerra ☃
 
04. SQL Servesdafr with CSharp WinForms.pdf
ManhHoangVan
 
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
Sql Server 2008 New Programmability Features
sqlserver.co.il
 
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
Sql storeprocedure
ftz 420
 
SQL-Demystified-A-Beginners-Guide-to-Database-Mastery.pptx
bhavaniteacher99
 
unit 3nit unit 1_unit1_unit1_.unit 1_unit1_unit1_
townhallforme1
 
Ad

More from elliando dias (20)

PDF
Clojurescript slides
elliando dias
 
PDF
Why you should be excited about ClojureScript
elliando dias
 
PDF
Functional Programming with Immutable Data Structures
elliando dias
 
PPT
Nomenclatura e peças de container
elliando dias
 
PDF
Geometria Projetiva
elliando dias
 
PDF
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
PDF
Javascript Libraries
elliando dias
 
PDF
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
PDF
Ragel talk
elliando dias
 
PDF
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
PDF
Introdução ao Arduino
elliando dias
 
PDF
Minicurso arduino
elliando dias
 
PDF
Incanter Data Sorcery
elliando dias
 
PDF
Rango
elliando dias
 
PDF
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
PDF
The Digital Revolution: Machines that makes
elliando dias
 
PDF
Hadoop + Clojure
elliando dias
 
PDF
Hadoop - Simple. Scalable.
elliando dias
 
PDF
Hadoop and Hive Development at Facebook
elliando dias
 
PDF
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 
Clojurescript slides
elliando dias
 
Why you should be excited about ClojureScript
elliando dias
 
Functional Programming with Immutable Data Structures
elliando dias
 
Nomenclatura e peças de container
elliando dias
 
Geometria Projetiva
elliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Javascript Libraries
elliando dias
 
How to Make an Eight Bit Computer and Save the World!
elliando dias
 
Ragel talk
elliando dias
 
A Practical Guide to Connecting Hardware to the Web
elliando dias
 
Introdução ao Arduino
elliando dias
 
Minicurso arduino
elliando dias
 
Incanter Data Sorcery
elliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
elliando dias
 
The Digital Revolution: Machines that makes
elliando dias
 
Hadoop + Clojure
elliando dias
 
Hadoop - Simple. Scalable.
elliando dias
 
Hadoop and Hive Development at Facebook
elliando dias
 
Multi-core Parallelization in Clojure - a Case Study
elliando dias
 
Ad

Recently uploaded (20)

PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Practical Applications of AI in Local Government
OnBoard
 
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 

A testing framework for Microsoft SQL-Server

  • 1. A testing framework for Microsoft SQL-Server Jorge Almeida
  • 2. Summary Database Testing in an RDBMS Unit test framework for databases Database manual testing Database automatized tests Database testing tools TSQLUnit framework How to write T-SQL unit tests Conclusions References
  • 4. Database Testing Categories of database tests Interface tests (validate what is going into, out of, and mapped) Internal database tests (validate the internal structure, behavior and contents) Internal database tests should validate: Database methods (e.g. stored procedures, functions, and triggers) Existence of database schema elements (tables, procedures, ...) View definitions Referential integrity rules Default values for a column Data invariants for a single column Data invariants involving several columns
  • 5. Database Manual Testing Combination of PRINT statements and ad-hoc tests with the SQL Query Analyzer Trace/debug tools like SQL Profiler Human judgment is required to analyze the results The tests can not easily be repeated again
  • 6. Database Automatized Tests Can be repeated over and over again Improve the quality of the code (refactoring) Serve the purpose of documentation May improve the design
  • 7. Database testing tools Category of database testing (interface and internal) Support the language of developing Help putting the database into a known state
  • 8. Some database testing tools Data Factory Datatect DTM Data Generator Turbo Data Developers need test data against which to validate their systems. Test data generators can be particularly useful when need large amounts of data, for stress and load testing Test data generator Empirix Mercury Interactive RadView Rational Suite Test Studio Web Performance Tools simulate high usage loads on database, enabling to determine whether the system’s architecture will stand up to true production needs Testing tools for load testing DBUnit NDbUnit OUnit for Oracle (being replaced soon by Qute ) SQLUnit TSQLUnit Visual Studio Team Edition for Database Professionals Tools which enable regression test Unit testing tools Examples Description Category
  • 9. TSQLUnit A testing framework for Microsoft SQL-Server It follows the tradition of the &quot;xUnit&quot; framework TSQLUnit is open source Developed by Henrik Ekelund Unit testing for stored procedures, functions, triggers and views Available from http://sourceforge.net/projects/tsqlunit Version 0.9 (December 15, 2002)
  • 10. TSQLUnit Install Download file from http://tsqlunit.sourceforge.net/tsqlunit_download.htm Unzip the file Connect to database as dbo using the SQL Query Analyzer, execute the tsqlunit.sql file
  • 11. Basic principles of database testing Database with good test data Shouldn’t developing against a production database Test one feature only per test procedure Be careful of how T-SQL comparison operators work When it is difficult to write tests change the design or refactor the system
  • 12. How to write T-SQL unit tests Create a stored procedure with a name that starts with ut and underscore (e.g. ut_testSomething ) Code a test, then call tsu_failure if test fails Execute tsu_runTests
  • 13. Test example (1) CREATE PROCEDURE ut_CapitalizeOneSentence AS BEGIN DECLARE @outStr VARCHAR (500) EXECUTE capitalize ‘ a string ’ , @outStr OUTPUT IF ASCII ( LEFT (@outStr,1)) <> ASCII ( ‘ A ’ ) OR @outStr IS NULL EXECUTE tsu_failure ‘ Capitalize should make the first character uppercase ’ END CREATE PROCEDURE capitalize @inStr VARCHAR (500)= NULL , @outStr VARCHAR (500)= NULL OUTPUT AS BEGIN SET @outStr= NULL IF @inStr IS NOT NULL SET @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
  • 14. Test example (2) CREATE PROCEDURE ut_CapitalizeNullSentence AS BEGIN DECLARE @outStr VARCHAR (500) EXECUTE capitalize NULL , @outStr OUTPUT IF @outStr IS NOT NULL EXECUTE tsu_failure ‘ Capitalize should not return any value ’ END CREATE PROCEDURE capitalize @inStr VARCHAR (500)= NULL , @outStr VARCHAR (500)= NULL OUTPUT AS BEGIN SET @outStr= NULL IF @inStr IS NOT NULL SET @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
  • 15. Test example (3) CREATE PROCEDURE ut_CapitalizeEmptySentence AS BEGIN DECLARE @outStr VARCHAR (500) EXECUTE capitalize ‘’ , @outStr OUTPUT IF @outStr<>’’ EXECUTE tsu_failure ‘ Capitalize should return the same value ’ END CREATE PROCEDURE capitalize @inStr VARCHAR (500)= NULL , @outStr VARCHAR (500)= NULL OUTPUT AS BEGIN SET @outStr= NULL IF @inStr IS NOT NULL SET @outStr= UPPER ( LEFT (@inStr,1))+ RIGHT (@inStr, LEN (@inStr)-1) END
  • 17. Improving tests Organize tests in classes ( suites ) (e.g. rename ut_CapitalizeEmptySentence as ut_Capitalize_EmptySentence then run the store procedure tsu_runTest ‘Capitalize’ ) Create a unique procedure for test setup (e.g. ut_Capitalize_SetUp) Create a unique procedure for test “teardown” (e.g. ut_Capitalize_TearDown)
  • 18. Conclusions Good unit testing tool for Microsoft SQL-Server stored procedures Regression test suite Some improvements needed (e.g. execution time of a single test) Be careful with error treatment in test store procedures
  • 19. References http://sourceforge.net/projects/tsqlunit http://www.tassq.org http://www.agile.org/essays/databaseTesting.html http://www.ambysoft.com/books/refactoringDatabases.html http:// www.ambysoft.com/books/agileDatabaseTechniques.html

Editor's Notes

  • #2: Este trabalho enquadra-se no grupo de trabalhos “Teste por camadas de aplicações empresariais”