SlideShare a Scribd company logo
Automated Unit and
Integration Testing with
Databases

      Stephen D. Ritchie
      Philly.NET Code Camp 2012.1
      12-May-2012
Chrysler New Yorker




                      Excella Consulting
Overview: Database “Units”




• Unit Testing Databases Is Difficult
    –   CRUD Operations on Tables
    –   Complex Querying of Multiple Tables
    –   Views
    –   Functions
    –   Stored Procedures
    –   Triggers !?!




                                Excella Consulting
Overview: Automated Testing Requirements




• Run Everywhere
    – Every developer must be able to run
      the combined collection of all the
      other developers’ tests.


• Continuous Integration
    – The CI server must be able to run
      the entire suite of tests without
      manual intervention.


• Deterministic, Isolated and Repeatable
    – The outcome of the tests must be unambiguous and repeatable.
      Usually, the standard is that if one test fails the entire test run fails.



                                  Excella Consulting
Overview: Databases Present Hurdles




• Automated Integration Testing with Databases

• This is Really Difficult

    – Schema Not Migrated/Versioned


    – Data Not In “Known-State”

    – Concurrency Control




                             Excella Consulting
RoundhousE




• Database Versioning and Change Management
   – Migrations Engine
   – Convention Over Configuration
• Apache License, Version 2.0
   – Free and Open Source
• NDbUnit Project
   – http://code.google.com/p/roundhouse/
• NuGet Package
   – http://nuget.org/packages/roundhouse
• Documentation
   – https://github.com/chucknorris/roundhouse/wiki




                             Excella Consulting
Tell Me More …




• How does RoundhousE work?

• Perhaps an example would be helpful …




                          Excella Consulting
Motivation




• Object Relational Mapping (ORM)
    – Entity Framework
    – NHibernate
    – Linq2Sql


• Linq Queries




                          Excella Consulting
NDbUnit




• Database Testing Framework
   – Borrows from the Java community’s DbUnit
   – “.NET library for managing database state during unit testing”
• Apache License, Version 2.0
          • Free and Open Source
• NDbUnit Project
   – http://code.google.com/p/ndbunit/
• NuGet Package
   – http://nuget.org/packages?q=NDbUnit
• Supports Many Databases
   – Microsoft SQL Server 2005 and 2008 (Express thru Enterprise)
   – Oracle (XE thru Enterprise, 9i and later)
   – SQLLite

                                   Excella Consulting
Tell Me More …




• How does NDbUnit work?

• Perhaps an example would be helpful …




                           Excella Consulting
Unit Testing Databases Is Difficult




• Unit Testing Databases Is Difficult
    –   CRUD Operations on Tables
    –   Complex Querying of Multiple Tables
    –   Views
    –   Functions
    –   Stored Procedures
    –   Triggers !?!


• Perhaps an example would be helpful …




                                Excella Consulting
Automated Integration Testing With Databases Is Really Difficult




• Automated Integration Testing With Databases Is Really Difficult
    – “Data Not In Known-State Before Test”


• Perhaps an example would be helpful …




                              Excella Consulting
Automated Testing The “Surface API”




• “Surface Testing”
    – DAL


• Perhaps an example
  would be helpful?




                            Excella Consulting
Integration Testing The ORM Interface




• “Surface Testing” Revisited
    – ORM Interface Surface


• NDbUnit
    – Independently Controls the
      Data Store




                              Excella Consulting
NDbUnit Downside




• Independence Has A Cost
   – Separately Defined The Schema
   – Separately Defined Each Known-Data-State


• Changing Schema
   – Updating DataSet
   – Updating XML Files




                            Excella Consulting
NDbUnit Upside




• Liberates Refactoring
    – Switch from CRUD Stored Procedures to ORM
    – Database Consolidation
    – Automated Integration Testing Legacy Code
        • Leap Forward
• Reporting
    – Test Report Queries Independent of Reporting Tool
• Browser Testing
    – Put Database In Known State
• Smoke, Stability, Performance, Regression and Other Testing
    – Automated Integration Testing




                              Excella Consulting
Another Option: Microsoft SQL Server




• tSQLt
    – Database Unit Testing Framework for SQL Server
    – Create & Execute Tests in
      SQL Server Management Studio
    – Free, Open Source
    – http://tsqlt.org/
• Red Gate: SQL Test
    –   Unit Test Add-In for SSMS
    –   Commercial
    –   “Powered by tSQLt” (API)
    –   http://www.red-gate.com/products/sql-development/sql-test/




                                Excella Consulting
Of Course It’s Safe … After You




                             Excella Consulting
Shameless Self Promotion Time!


                                        Get 40% off Pro .NET Best
                                        Practices!
                                        • Buy the eBook at
                                          Apress.com
                                        • Enter the promo
                                          code: PHN3T
                                        • Formats: PDF, ePub, or
                                          MOBI
                                        • Valid until May 31, 2012




                            Excella Consulting
More Shameless Self Promotion




• Code:        https://github.com/ruthlesshelp

• Slides:      http://www.slideshare.net/ruthlesshelp




                           Excella Consulting
Even More Shameless Self Promotion




• Email:       stephen.ritchie@excella.com

• Twitter:     @ruthlesshelp

• Blog:        http://ruthlesslyhelpful.net

• LinkedIn:    http://www.linkedin.com/in/sritchie

• Facebook:    http://www.facebook.com/ProDotNetBestPractices




                            Excella Consulting

More Related Content

PPTX
Automated Testing: Obstacles, Pitfalls, and Dangers
PPTX
Babysitting your orm essenmacher, adam
PPTX
Quack Chat | Fix Database Performance Problems with Profiling
PDF
SQL Tips + Tricks for Developers
PPTX
Automated Testing but like for PowerShell (April 2012)
PPTX
Testing in Legacy
PPTX
How to Use Innoslate for Beginners
PPTX
SQL Server Worst Practices - EN
Automated Testing: Obstacles, Pitfalls, and Dangers
Babysitting your orm essenmacher, adam
Quack Chat | Fix Database Performance Problems with Profiling
SQL Tips + Tricks for Developers
Automated Testing but like for PowerShell (April 2012)
Testing in Legacy
How to Use Innoslate for Beginners
SQL Server Worst Practices - EN

What's hot (17)

PDF
Strategic Testing (CodeMash 2016)
PPTX
Part of the DLM story: Get your Database under Source Control - SQL In The City
PPTX
Eurosport's Kodakademi #2
PDF
From Stairway to Heaven onto the Highway to Hell with Xtext
PPTX
Sql server infernals
PPTX
Working Effectively With Legacy Code
PDF
LF_APIStrat17_Don't Repeat Yourself - Your API is Your Documentation
PPTX
Get Testing with tSQLt - SQL In The City Workshop 2014
KEY
Xtext Best Practices
PPTX
Software requirements and estimates
PDF
Apache contribution-bar camp-colombo
PPTX
Unit Testing JavaScript with Jasmine
PDF
Clean tests
ODP
Finding the Bad Actor: Custom scoring & forensic name matching with Elastics...
PDF
[DevDay 2016] Design Pattern at a glance - Speaker: Tuan Do – Scrum Master a...
PDF
COE 2017: Your first 3DEXPERIENCE customization
PDF
Ideas spracklen-final
Strategic Testing (CodeMash 2016)
Part of the DLM story: Get your Database under Source Control - SQL In The City
Eurosport's Kodakademi #2
From Stairway to Heaven onto the Highway to Hell with Xtext
Sql server infernals
Working Effectively With Legacy Code
LF_APIStrat17_Don't Repeat Yourself - Your API is Your Documentation
Get Testing with tSQLt - SQL In The City Workshop 2014
Xtext Best Practices
Software requirements and estimates
Apache contribution-bar camp-colombo
Unit Testing JavaScript with Jasmine
Clean tests
Finding the Bad Actor: Custom scoring & forensic name matching with Elastics...
[DevDay 2016] Design Pattern at a glance - Speaker: Tuan Do – Scrum Master a...
COE 2017: Your first 3DEXPERIENCE customization
Ideas spracklen-final
Ad

Similar to Automated Testing with Databases (20)

PPTX
How to Build Deep Learning Models
PDF
20111110 how puppet-fits_into_your_existing_infrastructure_and_change_managem...
PDF
A data driven etl test framework sqlsat madison
PDF
Breaking data
PPTX
Rainbows, Unicorns, and other Fairy Tales in the Land of Serverless Dreams
PPTX
Agile Testing Best Practices
PPTX
Geek Sync | Deployment and Management of Complex Azure Environments
PPTX
Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing
PPTX
Lightweight Documentation: An Agile Approach
PDF
Storage Systems For Scalable systems
PPTX
DCDNUG 10/16/2012 Automated testing obstacles pitfalls dangers
PDF
SQL Server Clustering for Dummies
PPTX
Lean-Agile Development with SharePoint - Bill Ayers
PPTX
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
PDF
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
PPTX
From Pilot to Product - Morning@Lohika
KEY
33rd degree
PPT
5 Common Mistakes You are Making on your Website
PPT
ow.ppt
How to Build Deep Learning Models
20111110 how puppet-fits_into_your_existing_infrastructure_and_change_managem...
A data driven etl test framework sqlsat madison
Breaking data
Rainbows, Unicorns, and other Fairy Tales in the Land of Serverless Dreams
Agile Testing Best Practices
Geek Sync | Deployment and Management of Complex Azure Environments
Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing
Lightweight Documentation: An Agile Approach
Storage Systems For Scalable systems
DCDNUG 10/16/2012 Automated testing obstacles pitfalls dangers
SQL Server Clustering for Dummies
Lean-Agile Development with SharePoint - Bill Ayers
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
From Pilot to Product - Morning@Lohika
33rd degree
5 Common Mistakes You are Making on your Website
ow.ppt
Ad

More from Stephen Ritchie (14)

PPTX
Overview of .NET Best Practices
PPTX
Lightweight Documentation
PPTX
Agile Engineering Sparker GLASScon 2015
PPTX
DevOps Requires Agility
PPTX
Continuous Integration - NoVA CodeCamp 2014-10-11
PPTX
Continuous Integration: Blueprint, Toolbox, Master Craft
PDF
Dc scrum agile_eng_20130923
PPTX
Test Driven Development: Blueprint, Toolbox, and Master Craft
PPTX
Continuous Integration: Blueprint, Toolbox, Master Craft
PPTX
Continuous Integration DCAEC12
PPTX
An Overview of .NET Best Practices
PPTX
Advanced Code Analysis with .NET
PPTX
An Overview of .NET Best Practices
PPTX
Advanced Code Analysis In .NET
Overview of .NET Best Practices
Lightweight Documentation
Agile Engineering Sparker GLASScon 2015
DevOps Requires Agility
Continuous Integration - NoVA CodeCamp 2014-10-11
Continuous Integration: Blueprint, Toolbox, Master Craft
Dc scrum agile_eng_20130923
Test Driven Development: Blueprint, Toolbox, and Master Craft
Continuous Integration: Blueprint, Toolbox, Master Craft
Continuous Integration DCAEC12
An Overview of .NET Best Practices
Advanced Code Analysis with .NET
An Overview of .NET Best Practices
Advanced Code Analysis In .NET

Recently uploaded (20)

PDF
Modernizing your data center with Dell and AMD
PDF
Electronic commerce courselecture one. Pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Advanced IT Governance
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PPTX
Cloud computing and distributed systems.
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Modernizing your data center with Dell and AMD
Electronic commerce courselecture one. Pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity
Advanced IT Governance
madgavkar20181017ppt McKinsey Presentation.pdf
Cloud computing and distributed systems.
GamePlan Trading System Review: Professional Trader's Honest Take
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Chapter 3 Spatial Domain Image Processing.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced methodologies resolving dimensionality complications for autism neur...

Automated Testing with Databases

  • 1. Automated Unit and Integration Testing with Databases Stephen D. Ritchie Philly.NET Code Camp 2012.1 12-May-2012
  • 2. Chrysler New Yorker Excella Consulting
  • 3. Overview: Database “Units” • Unit Testing Databases Is Difficult – CRUD Operations on Tables – Complex Querying of Multiple Tables – Views – Functions – Stored Procedures – Triggers !?! Excella Consulting
  • 4. Overview: Automated Testing Requirements • Run Everywhere – Every developer must be able to run the combined collection of all the other developers’ tests. • Continuous Integration – The CI server must be able to run the entire suite of tests without manual intervention. • Deterministic, Isolated and Repeatable – The outcome of the tests must be unambiguous and repeatable. Usually, the standard is that if one test fails the entire test run fails. Excella Consulting
  • 5. Overview: Databases Present Hurdles • Automated Integration Testing with Databases • This is Really Difficult – Schema Not Migrated/Versioned – Data Not In “Known-State” – Concurrency Control Excella Consulting
  • 6. RoundhousE • Database Versioning and Change Management – Migrations Engine – Convention Over Configuration • Apache License, Version 2.0 – Free and Open Source • NDbUnit Project – http://code.google.com/p/roundhouse/ • NuGet Package – http://nuget.org/packages/roundhouse • Documentation – https://github.com/chucknorris/roundhouse/wiki Excella Consulting
  • 7. Tell Me More … • How does RoundhousE work? • Perhaps an example would be helpful … Excella Consulting
  • 8. Motivation • Object Relational Mapping (ORM) – Entity Framework – NHibernate – Linq2Sql • Linq Queries Excella Consulting
  • 9. NDbUnit • Database Testing Framework – Borrows from the Java community’s DbUnit – “.NET library for managing database state during unit testing” • Apache License, Version 2.0 • Free and Open Source • NDbUnit Project – http://code.google.com/p/ndbunit/ • NuGet Package – http://nuget.org/packages?q=NDbUnit • Supports Many Databases – Microsoft SQL Server 2005 and 2008 (Express thru Enterprise) – Oracle (XE thru Enterprise, 9i and later) – SQLLite Excella Consulting
  • 10. Tell Me More … • How does NDbUnit work? • Perhaps an example would be helpful … Excella Consulting
  • 11. Unit Testing Databases Is Difficult • Unit Testing Databases Is Difficult – CRUD Operations on Tables – Complex Querying of Multiple Tables – Views – Functions – Stored Procedures – Triggers !?! • Perhaps an example would be helpful … Excella Consulting
  • 12. Automated Integration Testing With Databases Is Really Difficult • Automated Integration Testing With Databases Is Really Difficult – “Data Not In Known-State Before Test” • Perhaps an example would be helpful … Excella Consulting
  • 13. Automated Testing The “Surface API” • “Surface Testing” – DAL • Perhaps an example would be helpful? Excella Consulting
  • 14. Integration Testing The ORM Interface • “Surface Testing” Revisited – ORM Interface Surface • NDbUnit – Independently Controls the Data Store Excella Consulting
  • 15. NDbUnit Downside • Independence Has A Cost – Separately Defined The Schema – Separately Defined Each Known-Data-State • Changing Schema – Updating DataSet – Updating XML Files Excella Consulting
  • 16. NDbUnit Upside • Liberates Refactoring – Switch from CRUD Stored Procedures to ORM – Database Consolidation – Automated Integration Testing Legacy Code • Leap Forward • Reporting – Test Report Queries Independent of Reporting Tool • Browser Testing – Put Database In Known State • Smoke, Stability, Performance, Regression and Other Testing – Automated Integration Testing Excella Consulting
  • 17. Another Option: Microsoft SQL Server • tSQLt – Database Unit Testing Framework for SQL Server – Create & Execute Tests in SQL Server Management Studio – Free, Open Source – http://tsqlt.org/ • Red Gate: SQL Test – Unit Test Add-In for SSMS – Commercial – “Powered by tSQLt” (API) – http://www.red-gate.com/products/sql-development/sql-test/ Excella Consulting
  • 18. Of Course It’s Safe … After You Excella Consulting
  • 19. Shameless Self Promotion Time! Get 40% off Pro .NET Best Practices! • Buy the eBook at Apress.com • Enter the promo code: PHN3T • Formats: PDF, ePub, or MOBI • Valid until May 31, 2012 Excella Consulting
  • 20. More Shameless Self Promotion • Code: https://github.com/ruthlesshelp • Slides: http://www.slideshare.net/ruthlesshelp Excella Consulting
  • 21. Even More Shameless Self Promotion • Email: [email protected] • Twitter: @ruthlesshelp • Blog: http://ruthlesslyhelpful.net • LinkedIn: http://www.linkedin.com/in/sritchie • Facebook: http://www.facebook.com/ProDotNetBestPractices Excella Consulting

Editor's Notes

  • #3: Introductory remarks.
  • #4: The database is a subsystem that is often treated like a “black box”.Development team often own the stored procedures, function, and triggers. They often have logic that satisfies an explicit or implicit requirement.In many organizations, the DBA team owns the table schema.Unit testing databases is difficult because a “unit”, such as a table, is hard to test in isolation. For example, your test code might need to use ADO.NET to query the table. Any number of configuration issues might prevent the test from passing. Besides, this is an integration test.Unit testing stored procedures is difficult because it is difficult to “Arrange” that the data in the tables is mocked so that it is in a known state before the test code executes the SP.