How to Perform Unit Testing in Oracle

Galaxy Glossary

How do I create and run automated unit tests for PL/SQL code in Oracle?

Oracle unit testing validates PL/SQL procedures, functions, and triggers by executing them in isolation and comparing actual results to expected outcomes.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why should I unit-test PL/SQL code?

Detect logic defects early, protect against regressions, and document expected behavior. Automated tests shorten release cycles and raise confidence in schema changes.

Which tools are available for Oracle unit testing?

Use native SQL Developer Unit Test, the open-source utPLSQL framework, or commercial suites like Quest Code Tester. This guide focuses on utPLSQL because it is free, scriptable, and CI-friendly.

How do I install utPLSQL quickly?

Clone the repository, connect as a privileged user, and run install_headless.sql.Grant UT_ROLE to developers running tests.

What is the basic test structure?

Create a schema-level package whose procedures start with test_. Inside, use ut.expect(actual).to_equal(expected). Register the package in a suite and execute with ut.run.

Example: Testing order total calculation

Suppose calc_order_total(p_order_id) in package orders_api returns the value stored in Orders.total_amount. A test verifies both value and datatype.

create or replace package orders_api_ut as
--%suite(Orders API)
--%suitepath(ecommerce).

--%test(Correct total returned)
procedure test_total_matches_column;
end orders_api_ut;
/
create or replace package body orders_api_ut as
procedure test_total_matches_column is
l_expected Orders.total_amount%type;
l_actual Orders.total_amount%type;
begin
select total_amount into l_expected from Orders where id = 1001;
l_actual := orders_api.calc_order_total(1001);
ut.expect(l_actual).to_equal(l_expected);
end;
end orders_api_ut;
/

How do I execute all tests?

Run begin ut.run(); end; in SQL*Plus, SQLcl, or any CI job.Pass ut_varchar2_list('ecommerce') to run a specific suite path.

How can I see a JUnit-style report?

Use the ut_xunit_reporter() parameter:
begin ut.run(reporters => ut_xunit_reporter()); end; Outputs XML consumable by Jenkins or GitLab.

Best practices for Oracle unit testing

Keep tests independent; reset data with savepoint/rollback or isolated test schemas. Cover edge cases—NULLs, constraint violations, bulk data. Integrate tests into every deployment pipeline.

What are common mistakes?

Relying on production data makes tests brittle; instead, insert minimal fixture rows.Ignoring performance leads to slow suites; prefer deterministic SQL over loops and avoid COMMIT inside tests.

How do I test triggers?

Fire DML inside the test, then query the table or audit log to assert side-effects. Wrap the DML in savepoint and rollback to leave no residue.

How do I mock external dependencies?

Replace calls to outbound web services or DB links with conditional compilation or test stubs in shadow packages.utPLSQL supports build-in spy helpers for verifying call counts.

Can I seed data with SQL scripts?

Yes. Use ut_suite_setup to run fixture DML once per suite, and ut_test_setup for each test case. Clean up in matching teardown procedures.

Next steps

Add coverage reports, integrate with Git hooks, and educate teammates. Strong test suites turn PL/SQL into a maintainable, reliable layer of your application.

.

Why How to Perform Unit Testing in Oracle is important

How to Perform Unit Testing in Oracle Example Usage


-- Verify Orders.total_amount via unit test
create or replace package orders_api_ut as
   --%suite(Orders API)
   --%suitepath(ecommerce)
   --%test(Returns accurate total)
   procedure test_total_matches_column;
end orders_api_ut;
/
create or replace package body orders_api_ut as
   procedure test_total_matches_column is
      l_expected Orders.total_amount%type;
      l_actual   Orders.total_amount%type;
   begin
      select total_amount into l_expected from Orders where id = 42;
      l_actual := orders_api.calc_order_total(42);
      ut.expect(l_actual).to_equal(l_expected);
   end;
end orders_api_ut;
/

How to Perform Unit Testing in Oracle Syntax


-- Install utPLSQL (run as SYS or DBA)
@https://raw.githubusercontent.com/utplsql/utPLSQL/master/install/utplsql_install_headless.sql

-- Skeleton of a test package
create or replace package <package_name> as
   --%suite(<suite_title>)
   --%suitepath(<hierarchy>)

   --%test(<test_description>)
   procedure test_<something>;
end <package_name>;
/

-- Run tests with optional reporters or paths
begin
   ut.run(
       a_path      => ut_varchar2_list('<suitepath>'),
       a_reporters => ut_varchar2_list(ut_documentation_reporter())
   );
end;
/

Common Mistakes

Frequently Asked Questions (FAQs)

Can I run utPLSQL from CI/CD pipelines?

Yes. utPLSQL offers CLI and Maven plugins that connect via JDBC, execute ut.run, and publish JUnit reports for Jenkins, GitLab, or GitHub Actions.

Does Oracle SQL Developer support unit tests?

SQL Developer includes a GUI that generates DBMS unit test harnesses. However, script-based utPLSQL is preferred for version control and automation.

How do I test PL/SQL functions that return cursors?

Fetch the cursor into a collection, then compare counts, data types, or specific columns with ut.expect helpers like ut.equal and ut.to_raise.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.