SlideShare a Scribd company logo
PL/SQL Unit Testing
   Can Be Fun!
     with a little help
        from Ruby
Raimonds Simanovskis

github.com/
    rsim
What is good
  code?
                   Test
  Correct
               functionality!
                   Test
Fast enough
               performance!
                 Test after
Maintainable
                 changes!
How is it done?
        try something...

    dbms_output.put_line(...)

 it is obvious that there are no bugs


testers should test,
   that’s their job
What are typical
       problems?
        Trying and not testing

       Tests are not repeatable

     Manual verification of results

Testing is done too late in development
Types of tests
         unit tests

      integration tests
                                programmer’s
performance & integration
                                responsibility
         tests

exploratory & usability tests
Test Driven Development
Good unit tests
           Automatic, run fast

Wide code coverage including edge cases

              Repeatable

   Independent from execution order

 Using real and understandable test data
How to do it when
 programming in
    PL/SQL?
created by Steven Feuerstein, 1999

based on “xUnit”-style frameworks

    not maintained anymore :(
Example
    substring from start until end position
CREATE OR REPLACE FUNCTION betwnstr (
   string_in   IN   VARCHAR2,
   start_in    IN   INTEGER,
   end_in      IN   INTEGER
)
   RETURN VARCHAR2
IS
   l_start PLS_INTEGER := start_in;
BEGIN
   IF l_start = 0
   THEN
      l_start := 1;
   END IF;

   RETURN (SUBSTR (string_in, l_start, end_in - l_start + 1));
END;
Tests
CREATE OR REPLACE PACKAGE ut_betwnstr AS   CREATE OR REPLACE PACKAGE BODY ut_betwnstr AS

  PROCEDURE ut_setup;                        PROCEDURE ut_setup AS
  PROCEDURE ut_teardown;                     BEGIN
                                               NULL;
  PROCEDURE ut_normal_usage;                 END ut_setup;
  PROCEDURE ut_first_index_null;
                                             PROCEDURE ut_teardown AS
END ut_betwnstr;                             BEGIN
                                               NULL;
                                             END ut_teardown;

                                             PROCEDURE ut_normal_usage AS
                                             BEGIN
                                               utassert.eq('Normal usage',
                                                 betwnstr('abcdefg', 2, 5),
                                                 'bcde');
                                             END ut_normal_usage;

                                             PROCEDURE ut_first_index_null AS
                                             BEGIN
                                               utassert.isnull('First index is null',
                                                 betwnstr('abcdefg', NULL, 5));
                                             END ut_first_index_null;

                                           END ut_betwnstr;
Results
                 exec utplsql.test('betwnstr', recompile_in => FALSE);
.
>        SSSS      U         U     CCC        CCC       EEEEEEE     SSSS         SSSS
>       S      S U           U     C     C     C     C E          S      S     S      S
>     S            U         U   C         C C        C E       S            S
>       S          U         U   C           C          E         S            S
>         SSSS     U         U   C           C          EEEE        SSSS         SSSS
>              S U           U   C           C          E                S            S
>                S U         U   C         C C        C E                  S            S
>       S      S     U     U       C     C     C     C E          S      S     S      S
>         SSSS         UUU           CCC         CCC    EEEEEEE     SSSS         SSSS
.
    SUCCESS: "betwnstr"
.
> Individual Test Case Results:
>
SUCCESS - betwnstr.UT_FIRST_INDEX_NULL: ISNULL "First index is null" Expected "" and got ""
>
SUCCESS - betwnstr.UT_NORMAL_USAGE: EQ "Normal usage" Expected "cde" and got "cde"
>
>
> Errors recorded in utPLSQL Error Log:
>
> NONE FOUND
Visual testing tools
Quest Code Tester   SQL Developer 2.1
Why used just by few?
 Too large / too verbose test code?

   Hard to read, too much noise?

    Hard to test complex cases?

No best practices how to write tests?

 Nobody is using, why should I use?
ruby-plsql-spec
                ideal language
                for writing tests

             powerful testing tools
 RSpec       with “readable” syntax

               library for calling
ruby-plsql    PL/SQL procedures
                   from Ruby
Demo
ruby-plsql gem
plsql.connect! "hr","hr","xe"

plsql.test_uppercase('xxx')              # => "XXX"
plsql.test_uppercase(:p_string => 'xxx') # => "XXX"
plsql.test_copy("abc", nil, nil)         # => { :p_to => "abc",
                                         # :p_to_double => "abcabc" }
plsql.test_copy(:p_from => "abc", :p_to => nil, :p_to_double => nil)
                                         # => { :p_to => "abc",
                                         # :p_to_double => "abcabc" }
plsql.hr.test_uppercase('xxx')           # => "XXX"
plsql.test_package.test_uppercase('xxx') # => 'XXX'
plsql.hr.test_package.test_uppercase('xxx') # => 'XXX'

plsql.logoff
Benefits
      Compact, readable syntax

Powerful features also for complex tests

 Best practices from Ruby community

  Based on needs from real projects

   Open-source – “free as in beer” :)
More information
 http://blog.rayapps.com/2009/11/27/oracle-plsql-
                unit-testing-with-ruby/

http://blog.rayapps.com/2010/01/06/screencasts-of-
          oracle-plsql-unit-testing-with-ruby/


    http://github.com/rsim/ruby-plsql-spec
JRuby meetup
      Tuesday, September 21

5:30 - 7:00 Networking + beer + food
7:00 - 8:00 Lightning talks


         @ Engine Yard
     500 3rd Street, Suite 510

More Related Content

PDF
PL/SQL Unit Testing Can Be Fun
PDF
Testing JavaScript Applications
PDF
Adventures In JavaScript Testing
PDF
Unit testing with mocha
PDF
Unit testing JavaScript using Mocha and Node
PDF
Jasmine - why JS tests don't smell fishy
PPTX
Javascript Testing with Jasmine 101
PDF
Painless JavaScript Testing with Jest
PL/SQL Unit Testing Can Be Fun
Testing JavaScript Applications
Adventures In JavaScript Testing
Unit testing with mocha
Unit testing JavaScript using Mocha and Node
Jasmine - why JS tests don't smell fishy
Javascript Testing with Jasmine 101
Painless JavaScript Testing with Jest

What's hot (20)

PDF
Workshop 5: JavaScript testing
PDF
Unit Testing Express Middleware
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
PDF
Practical Celery
PDF
Introduction to Protractor
KEY
Testing JS with Jasmine
PDF
PDF
Testing javascript in the frontend
PDF
Jasmine BDD for Javascript
PPT
Testing Javascript with Jasmine
PPTX
Workshop 1: Good practices in JavaScript
PPT
Testing persistence in PHP with DbUnit
PDF
Browser testing with nightwatch.js - Drupal Europe
PDF
Why Task Queues - ComoRichWeb
PPTX
Full Stack Unit Testing
PDF
Beginning PHPUnit
ODP
Europython 2011 - Playing tasks with Django & Celery
PDF
50 new features of Java EE 7 in 50 minutes
PDF
JavaScript TDD with Jasmine and Karma
PDF
Understanding JavaScript Testing
Workshop 5: JavaScript testing
Unit Testing Express Middleware
Advanced Jasmine - Front-End JavaScript Unit Testing
Practical Celery
Introduction to Protractor
Testing JS with Jasmine
Testing javascript in the frontend
Jasmine BDD for Javascript
Testing Javascript with Jasmine
Workshop 1: Good practices in JavaScript
Testing persistence in PHP with DbUnit
Browser testing with nightwatch.js - Drupal Europe
Why Task Queues - ComoRichWeb
Full Stack Unit Testing
Beginning PHPUnit
Europython 2011 - Playing tasks with Django & Celery
50 new features of Java EE 7 in 50 minutes
JavaScript TDD with Jasmine and Karma
Understanding JavaScript Testing
Ad

Viewers also liked (20)

PDF
PL SQL Unit Tests mit SQL Developer
PPTX
Oracle Unit Testing with utPLSQL
PPTX
utPLSQL: Unit Testing for Oracle PL/SQL
ODP
Intro to Algebra II
PPT
Orbital Notation
PPTX
Top 10 senior administrative officer interview questions and answers
PDF
Packet capture and network traffic analysis
PPT
Virtualization In Software Testing
PPT
Video Quality Measurements
PPTX
Vendor quality management
PDF
Digital Platform Selection Best Practices
PPT
Analysis of water pollution presentaion by m.nadeem ashraf
PDF
Hands-On Lab: Let's Build an ITSM Dashboard
PPT
Defining Workplace Safety
PPTX
Which test cases to automate
DOCX
Str581 final exam part 1
RTF
Chem Lab Report (1)
PPTX
E leave management-system
PDF
Catalogo de Productos Nutrilite (Amway)
PDF
Telecom Roaming Overview
PL SQL Unit Tests mit SQL Developer
Oracle Unit Testing with utPLSQL
utPLSQL: Unit Testing for Oracle PL/SQL
Intro to Algebra II
Orbital Notation
Top 10 senior administrative officer interview questions and answers
Packet capture and network traffic analysis
Virtualization In Software Testing
Video Quality Measurements
Vendor quality management
Digital Platform Selection Best Practices
Analysis of water pollution presentaion by m.nadeem ashraf
Hands-On Lab: Let's Build an ITSM Dashboard
Defining Workplace Safety
Which test cases to automate
Str581 final exam part 1
Chem Lab Report (1)
E leave management-system
Catalogo de Productos Nutrilite (Amway)
Telecom Roaming Overview
Ad

Similar to PL/SQL Unit Testing Can Be Fun! (20)

PDF
Testing Code and Assuring Quality
PDF
Developer Tests - Things to Know (Vilnius JUG)
PDF
Developer Test - Things to Know
PDF
Ansible testing
PDF
Describe's Full of It's
PDF
Introduction To Testing With Perl
PPTX
A New View of Database Views
PPT
A testing framework for Microsoft SQL-Server
ODP
Performance tuning
KEY
iOS Unit Testing
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
PDF
TSQL Coding Guidelines
PPTX
Test driven development (java script & mivascript)
PDF
Introduction to unit testing
PDF
Hidden Gems of Ruby 1.9
KEY
Building and Distributing PostgreSQL Extensions Without Learning C
PDF
Automated tests - facts and myths
PDF
Testing With Test::Class
PDF
PhpUnit Best Practices
PDF
Effective Unit Test Style Guide
Testing Code and Assuring Quality
Developer Tests - Things to Know (Vilnius JUG)
Developer Test - Things to Know
Ansible testing
Describe's Full of It's
Introduction To Testing With Perl
A New View of Database Views
A testing framework for Microsoft SQL-Server
Performance tuning
iOS Unit Testing
We Are All Testers Now: The Testing Pyramid and Front-End Development
TSQL Coding Guidelines
Test driven development (java script & mivascript)
Introduction to unit testing
Hidden Gems of Ruby 1.9
Building and Distributing PostgreSQL Extensions Without Learning C
Automated tests - facts and myths
Testing With Test::Class
PhpUnit Best Practices
Effective Unit Test Style Guide

More from Raimonds Simanovskis (20)

PDF
Profiling Mondrian MDX Requests in a Production Environment
PDF
Improve Mondrian MDX usability with user defined functions
PDF
Analyze and Visualize Git Log for Fun and Profit - DevTernity 2015
PDF
Data Warehouses and Multi-Dimensional Data Analysis
PDF
mondrian-olap JRuby library
PDF
eazyBI Overview - Embedding Mondrian in other applications
PDF
Atvērto datu izmantošanas pieredze Latvijā
PDF
JavaScript Unit Testing with Jasmine
PDF
JRuby - Programmer's Best Friend on JVM
PDF
Agile Operations or How to sleep better at night
PDF
TDD - Why and How?
PDF
Analyze and Visualize Git Log for Fun and Profit
PDF
opendata.lv Case Study - Promote Open Data with Analytics and Visualizations
PDF
Extending Oracle E-Business Suite with Ruby on Rails
PDF
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
PDF
RailsWayCon: Multidimensional Data Analysis with JRuby
PDF
Why Every Tester Should Learn Ruby
PDF
Multidimensional Data Analysis with JRuby
PDF
Rails on Oracle 2011
PDF
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Profiling Mondrian MDX Requests in a Production Environment
Improve Mondrian MDX usability with user defined functions
Analyze and Visualize Git Log for Fun and Profit - DevTernity 2015
Data Warehouses and Multi-Dimensional Data Analysis
mondrian-olap JRuby library
eazyBI Overview - Embedding Mondrian in other applications
Atvērto datu izmantošanas pieredze Latvijā
JavaScript Unit Testing with Jasmine
JRuby - Programmer's Best Friend on JVM
Agile Operations or How to sleep better at night
TDD - Why and How?
Analyze and Visualize Git Log for Fun and Profit
opendata.lv Case Study - Promote Open Data with Analytics and Visualizations
Extending Oracle E-Business Suite with Ruby on Rails
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
RailsWayCon: Multidimensional Data Analysis with JRuby
Why Every Tester Should Learn Ruby
Multidimensional Data Analysis with JRuby
Rails on Oracle 2011
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
sap open course for s4hana steps from ECC to s4
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
PDF
Electronic commerce courselecture one. Pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KodekX | Application Modernization Development
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
sap open course for s4hana steps from ECC to s4
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
Electronic commerce courselecture one. Pdf
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Mobile App Security Testing_ A Comprehensive Guide.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

PL/SQL Unit Testing Can Be Fun!

  • 1. PL/SQL Unit Testing Can Be Fun! with a little help from Ruby
  • 3. What is good code? Test Correct functionality! Test Fast enough performance! Test after Maintainable changes!
  • 4. How is it done? try something... dbms_output.put_line(...) it is obvious that there are no bugs testers should test, that’s their job
  • 5. What are typical problems? Trying and not testing Tests are not repeatable Manual verification of results Testing is done too late in development
  • 6. Types of tests unit tests integration tests programmer’s performance & integration responsibility tests exploratory & usability tests
  • 8. Good unit tests Automatic, run fast Wide code coverage including edge cases Repeatable Independent from execution order Using real and understandable test data
  • 9. How to do it when programming in PL/SQL?
  • 10. created by Steven Feuerstein, 1999 based on “xUnit”-style frameworks not maintained anymore :(
  • 11. Example substring from start until end position CREATE OR REPLACE FUNCTION betwnstr ( string_in IN VARCHAR2, start_in IN INTEGER, end_in IN INTEGER ) RETURN VARCHAR2 IS l_start PLS_INTEGER := start_in; BEGIN IF l_start = 0 THEN l_start := 1; END IF; RETURN (SUBSTR (string_in, l_start, end_in - l_start + 1)); END;
  • 12. Tests CREATE OR REPLACE PACKAGE ut_betwnstr AS CREATE OR REPLACE PACKAGE BODY ut_betwnstr AS PROCEDURE ut_setup; PROCEDURE ut_setup AS PROCEDURE ut_teardown; BEGIN NULL; PROCEDURE ut_normal_usage; END ut_setup; PROCEDURE ut_first_index_null; PROCEDURE ut_teardown AS END ut_betwnstr; BEGIN NULL; END ut_teardown; PROCEDURE ut_normal_usage AS BEGIN utassert.eq('Normal usage', betwnstr('abcdefg', 2, 5), 'bcde'); END ut_normal_usage; PROCEDURE ut_first_index_null AS BEGIN utassert.isnull('First index is null', betwnstr('abcdefg', NULL, 5)); END ut_first_index_null; END ut_betwnstr;
  • 13. Results exec utplsql.test('betwnstr', recompile_in => FALSE); . > SSSS U U CCC CCC EEEEEEE SSSS SSSS > S S U U C C C C E S S S S > S U U C C C C E S S > S U U C C E S S > SSSS U U C C EEEE SSSS SSSS > S U U C C E S S > S U U C C C C E S S > S S U U C C C C E S S S S > SSSS UUU CCC CCC EEEEEEE SSSS SSSS . SUCCESS: "betwnstr" . > Individual Test Case Results: > SUCCESS - betwnstr.UT_FIRST_INDEX_NULL: ISNULL "First index is null" Expected "" and got "" > SUCCESS - betwnstr.UT_NORMAL_USAGE: EQ "Normal usage" Expected "cde" and got "cde" > > > Errors recorded in utPLSQL Error Log: > > NONE FOUND
  • 14. Visual testing tools Quest Code Tester SQL Developer 2.1
  • 15. Why used just by few? Too large / too verbose test code? Hard to read, too much noise? Hard to test complex cases? No best practices how to write tests? Nobody is using, why should I use?
  • 16. ruby-plsql-spec ideal language for writing tests powerful testing tools RSpec with “readable” syntax library for calling ruby-plsql PL/SQL procedures from Ruby
  • 17. Demo
  • 18. ruby-plsql gem plsql.connect! "hr","hr","xe" plsql.test_uppercase('xxx') # => "XXX" plsql.test_uppercase(:p_string => 'xxx') # => "XXX" plsql.test_copy("abc", nil, nil) # => { :p_to => "abc", # :p_to_double => "abcabc" } plsql.test_copy(:p_from => "abc", :p_to => nil, :p_to_double => nil) # => { :p_to => "abc", # :p_to_double => "abcabc" } plsql.hr.test_uppercase('xxx') # => "XXX" plsql.test_package.test_uppercase('xxx') # => 'XXX' plsql.hr.test_package.test_uppercase('xxx') # => 'XXX' plsql.logoff
  • 19. Benefits Compact, readable syntax Powerful features also for complex tests Best practices from Ruby community Based on needs from real projects Open-source – “free as in beer” :)
  • 20. More information http://blog.rayapps.com/2009/11/27/oracle-plsql- unit-testing-with-ruby/ http://blog.rayapps.com/2010/01/06/screencasts-of- oracle-plsql-unit-testing-with-ruby/ http://github.com/rsim/ruby-plsql-spec
  • 21. JRuby meetup Tuesday, September 21 5:30 - 7:00 Networking + beer + food 7:00 - 8:00 Lightning talks @ Engine Yard 500 3rd Street, Suite 510