Database DevOps for Oracle

Unit testing

Redgate recommends utPLSQL, an open source (Apache2) unit testing frame for Oracle with SQL Developer integration command line and built-in code coverage support. 

Showing utPLSQL tests in Jenkins

Resources:

utPLSQL

SQL Developer Unit testing

 utPLSQL Getting started

  1. Download and unzip utPLSQL to a folder in your working folder.
  2. Follow the manual installation procedure using the scripts in the source sub folder.
  3. Create some tests.

This assumes that you have downloaded and unzipped utPLSQL.zip

Install and Setup Toggle source code

  1. -- Run this from the utPLSQL\source folder
  2. sqlplus SOCO_DEV/demopassword@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=localhost)(Port=1521))(CONNECT_DATA=(SID=XE))) as sysdba @create_utPLSQL_owner ut3 ut3 users
  3. sqlplus SOCO_DEV/demopassword@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=localhost)(Port=1521))(CONNECT_DATA=(SID=XE))) @install.sql ut3
  4. sqlplus SOCO_DEV/demopassword@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=localhost)(Port=1521))(CONNECT_DATA=(SID=XE))) @create_synonyms_and_grants_for_public.sql ut3

Sample tests.

The following conventions must be adhered to for the framework to pick up the tests correctly:

  1. Packages that contain tests need to be prefixed with test_
  2. In the package, provide:
    1. -- %suite(<a name for the test suite>)
      and above each test procedure: 

    2. -- %test(<a description for the test>)

Sample utPLSQL test code Toggle source code

  1. -- sample tests from utPLSQL documentation
  2.  
  3. create or replace package test_betwnstr as
  4.  
  5. -- %suite(Between string function)
  6.  
  7. -- %test(Returns substring from start position to end position)
  8. procedure basic_usage;
  9.  
  10. -- %test(Returns substring when start position is zero)
  11. procedure zero_start_position;
  12.  
  13. end;
  14. /
  15.  
  16. create or replace package body test_betwnstr as
  17.  
  18. procedure basic_usage is
  19. begin
  20. ut.expect( betwnstr( '1234567', 2, 5 ) ).to_equal('2345');
  21. end;
  22.  
  23. procedure zero_start_position is
  24. begin
  25. ut.expect( betwnstr( '1234567', 0, 5 ) ).to_equal('12345');
  26. end;
  27.  
  28. end;
  29. /
  30.  
  31. create or replace function betwnstr( a_string varchar2, a_start_pos integer, a_end_pos integer ) return varchar2
  32. is
  33. begin
  34. if a_start_pos = 0 then
  35. return substr( a_string, a_start_pos, a_end_pos - a_start_pos );
  36. else
  37. return substr( a_string, a_start_pos, a_end_pos - a_start_pos + 1);
  38. end if;
  39. end;
  40. /
  41.  
  42. set serveroutput on;
  43. exec ut.run('test_betwnstr');

How to call the utPLSQL command line from a build-test batch file:

  1. Download the command line from https://github.com/utPLSQL/utPLSQL-cli/releases
  2. Copy ojdbc8.jar and orai18n.jar from Oracle's download page and place in \utPLSQL-cli\lib


utPLSQL command line Toggle source code

  1. call Tools\utPLSQL-cli\bin\utplsql run SOCO_TEST/demopassword@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=localhost)(Port=1521))(CONNECT_DATA=(SID=XE))) -f=ut_xunit_reporter -o=Artifacts/test_results.xml

How to integrate JUnit test results with Jenkins (in the worked example this is called from the Jenkinsfile)

Publishing JUnit test results Toggle source code

  1. junit 'Artifacts/*.xml'

Didn't find what you were looking for?