PL/SQL Unit Testing Can Be Fun
Upcoming SlideShare
Loading in...5

PL/SQL Unit Testing Can Be Fun



Presentation at Miracle Open World 2012

Presentation at Miracle Open World 2012



Total Views
Slideshare-icon Views on SlideShare
Embed Views



3 Embeds 3 1 1 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    PL/SQL Unit Testing Can Be Fun PL/SQL Unit Testing Can Be Fun Presentation Transcript

    • PL/SQL Unit Testing Can Be Fun! with a little help from Ruby
    • Raimonds Simanovskis @rsim .com
    • What is good code? Test Correct functionality! TestFast enough performance! Test afterMaintainable changes!
    • How is it done? try something... dbms_output.put_line(...) it is obvious that there are no bugstesters should test, that’s their job
    • What are typical problems? Trying and not testing Tests are not repeatable Manual verification of resultsTesting is done too late in development
    • Types of tests unit tests integration tests programmer’s performance & load responsibility testsexploratory & usability tests
    • Test Driven Development
    • Good unit tests Automatic, run fastWide 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, 1999based on “xUnit”-style frameworks not maintained anymore :(
    • Example substring from start until end positionCREATE OR REPLACE FUNCTION betwnstr ( string_in IN VARCHAR2, start_in IN INTEGER, end_in IN INTEGER) RETURN VARCHAR2IS 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;
    • TestsCREATE 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 ASEND 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 toolsQuest 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 callingruby-plsql PL/SQL procedures from Ruby
    • Demo
    • ruby-plsql gemplsql.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" } # => "XXX"plsql.test_package.test_uppercase(xxx) # => # => XXXplsql.logoff
    • Benefits Compact, readable syntaxPowerful features also for complex tests Best practices from Ruby community Based on needs from real projects Open-source – “free as in beer” :)
    • More information unit-testing-with-ruby/ oracle-plsql-unit-testing-with-ruby/