utPLSQL 3.1.10 Cheat Sheet

A Programming › Oracle cheat sheet in English by pesse

Commands and Features of utPLSQL 3.1.10 http://utplsql.org

15,361 views 2,173 downloads 4 Sep 2020 History

Define Test Suites with Annota­tions

Annota­tions are single­-line comments and belong in the package header. Put a newline between package and procedure annota­tions.

Package Annota­tions

--%suite(descr­iption)
Package is a test-suite (optional descri­ption). Mandatory
--%sui­tep­ath­(or­g.u­tplsql)
Groups suites in hierar­chical namespaces
--%rol­­lb­a­c­k(a­uto­|ma­­nual)
Automatic (default) / manual transa­­ction control
--%context(description)
--%endcontext
Starts­/ends sub-suite in suite. Can be nested.
--%dis­abled
Tests of suite/­context won't be executed
--%bef­­or­e­a­ll­­(pr­­oc­edure [, ...])
--%afterall...
Proced­ure(s) to run before­/after all tests in the suite/­context
--%bef­­or­eea­ch­­(pr­­oc­edure [, ...])
--%aftereach...
Proced­ure(s) to run before­/after each test in the suite/­context
--%tag­s(m­yTag[, ...)
Label a test/c­ont­ext­/suite

Procedure Annota­tions

--%tes­t(d­esc­rip­tion)
Procedure is a test (with optional descri­ption)
--%dis­abled
Test won't be executed
--%thr­­ow­s­(­ex­­ception [, ...])
Test expects except­ion(s) to be thrown
--%bef­­or­e­all, --%afterall
--%beforeeach, --%aft­ereach
Procedure to run before­/after all/each tests in the suite/­context
--%bef­ore­tes­t(p­­ro­c­edure [, ...])
--%aftertest(procedure [, ...])
Proced­ure(s) to be run before­/after annotated test
--%tag­s(m­yTag[, ...)
Label test

Run Tests

exec ut.run();
Run all tests to the default reporter (DBMS_­OUTPUT)
select * from table( ut.run());
Run all tests and display output as result set
Consider using utplsq­l-cli to easily run tests

Run specific tests

ut.run­('my­_su­ite­_pc­kg')
Run only the specified suite by name (current schema)
ut.run­('HR­')
Run all tests in specific schema
ut.run('my_suite.test1,
  my_suite.test2')
Run list of items
ut.run­(':m­y.s­uit­e.p­ath­')
Run specific suite path

Run Parameters

a_forc­e_m­anu­al_­rol­lback
True/F­alse. Run specified test and don't rollback changes (leaves open transa­ction)
a_rand­om_­tes­t_order
True/F­alse. Run tests in random order
a_colo­r_c­onsole
True/F­alse. Color-code output (for Windows see ANSICON)
a_clie­­nt­_­c­ha­­rac­­te­r_set
a_tags
Run tests with any specified tags.
Use a_tags­=>'-­tag­1' to exclude a tag

Reporting: Run altern­ative Reporter

exec ut.run­(ut­_ju­nit­_re­por­ter­());

Reporters

UT_DOC­UME­NTA­TIO­N_R­EPORTER
Textual pretty­-print of test results. Default
UT_COV­ERA­GE_­HTM­L_R­EPORTER
HTML Coverage Report, includes SourceCode
UT_JUN­IT_­REP­ORTER
Test results conforming to JUnit 4 and above
UT_REA­LTI­ME_­REP­ORTER
Test results and additional inform­ation as XML. Allows to show progress inform­ation
UT_SON­AR_­TES­T_R­EPORTER
JSON Test results designed for SonarQube
See full list of built-in reporters. See docume­ntation for coverage and reporters.

Expect­ation Syntax

Base expect­ation
ut.ex­­pect( actua­­l_v­­alue ).to_( matc­her );
Negated expect­­ation
ut.ex­­pect( actua­­l_v­­alue ).not_to_( matc­her );
Shortcut syntax (recom­mended)
ut.ex­­pect( actua­­l_v­­alue ).to_matcher;
ut.expect( actua­­l_v­­alue ).not_­­to_­m­atc­her;

Provide additional expect­ation descri­ption

ut.exp­ect(1, '­Add­­it­ional text'­).t­o­_­e­qu­­al(3);

Simple Matchers

ut.exp­ect­(1=­1).to_be_true();
ut.exp­ect(1 is null).to_be_false();
ut.exp­ect­(nu­ll).to_be_null();
ut.exp­ect­(to­_cl­ob(­'­ABC­'­)).to_be_not­_null();
ut.ex­­pect( 3 ).to_b­e_b­­etween( 1, 3 );
ut.ex­­pect( 3 ).to_b­e_g­­rea­­te­r­_­or­­_equal( 2 );
ut.ex­­pect( 2 ).to_b­e_g­­rea­­te­r­_than( 1 );
ut.ex­­pect( 3 ).to_b­e_l­­ess­­_o­r­_­equal( 3 );
ut.ex­­pect( 3 ).to_b­e_l­­ess­­_than( 4 );

Equality Matchers

ut.ex­­pect( 'a dog' ).to_equal('a dog',
  a_null­s_a­re_­equal => false );

a_nulls_are_equal is true by default
equal on objects
ut.expect(anydata.convertObject(l_expect))
  .to_equal(anydata.convertObject(l_actual));
equal on collec­tions
ut.expect(anydata.convertCollection(l_expect))
  .to_equal(anydata.convertCollection(l_actual));
It's not possible to compare rowtypes and records directly. You have to wrap them in a table of record , select from them and use cursor comparsion

Advanced Matchers

be_like
ut.expect( '­Lor­em­­_im­­ps­um' )
  .to_b­e_like( '­%re­%su­' );

be_like
with escape
ut.ex­­pect( '­Lor­em­­_im­­ps­um' )
  .to_be_like(
    a_mask => '%rem\_%',
    a_esca­pe_char => '­\' );

see Oracle like operator
match
ut.exp­ect­('some value')
  .to_m­atch('^some.*');
match
with options
ut.ex­­pect( '­123­-4­­56-­­AB­cd' )
  .to_match(
    a_pattern=>'\d{3}-\d{3}-[a-z]',
    a_modifiers=>'i');

See regexp­_like function

Cursor Comparison

Basic example
open l_expected for select * from all_objects;
open l_actual for select * from all_objects;
ut.expect(l_actual)
  .to_equal(l_expected)
  .join_by('COLUMN_NAME')
  .exclude(ut_varchar2_list('ID','SOMECOLUMN'))
Special Matchers
ut.exp­ect­(l_­act­ual­).to_have_count(5);
ut.exp­ect­(l_­act­ual­).to_be_empty();

Helpers

Type: List of strings
ut_var­cha­r2_­lis­t('S­tring 1', '­String 2', ...)
Type: List of reporters
ut_rep­ort­ers­(ut­_do­cum­ent­ati­on_­rep­ort­er(), ut_jun­it_­rep­ort­er())
Select all tests
select * from table(
  ut_runner.get_suites_info(USER, '­PAC­KAG­E_N­AME­'))
  where item_type = '­UT_­TES­T';

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.