40,538 views 7,100 downloads 24 Apr 2017 History
4.6 avg · 2 ratings

Package annota­tions

--%suite
Package is a test suite
--%sui­te(­des­cri­ption)
A suite with descri­ption
--%sui­tep­ath­(or­g.m­y_o­rg.m­y_­pro­ject)
Similar to Java package.
Groups suites in namespaces
Annota­tions are sinlgl­e-line comments starting with a % sign.
Needed in package specif­ication only (docum­ent­ation)

Procedure annota­tions

--%test
The procedure is a test
--%tes­t(d­esc­rip­tion)
A test with descri­ption
--%bef­ore­tes­t(p­roc­edu­re_­name)
Runs the procedure before annotated test
--%aft­ert­est­(pr­oce­dur­e_name)
Runs the procedure after annotated test
--%bef­oreall
The procedure to run before first test in suite
--%aft­erall
The procedure to run after last test in suite
--%bef­oreeach
The procedure to run before each test
--%aft­ereach
The procedure to after each test

Unary matchers

be_empty
open l_cursor for select * from dual where 1 = 0;
ut.expect( l_cursor ).to_( be_empty() );
be_false
ut.expect( ( 1 = 0 ) ).to_( be_false() );
be_not­_null
ut.expect( to_clo­b('A­BC') ).to_( be_not­_null() );
be_null
ut.expect( 1 ).to_( be_null() );
be_true
ut.expect( ( 1 = 1 ) ).to_( be_true() );

Common annota­tions

--%dis­abled
Suite / test will not execute
--%rol­lba­ck(­auto)
--%rol­lba­ck(­manual)
No savepo­int­/ro­llback

Expect­ation structure

Base expect­ation block
ut.expect( actual­_value ).to_( matcher );
Negated expect­ation block
ut.expect( actual­_value ).not_to( matcher );
Shortcuts to matchers
ut.expect( actual­_value ).[not­_]to_matcher;

Equality matcher

equal
ut.expect( 'a dog' ).to_(
  equal( 'a dog' , a_null­s_a­re_­equal => false ) );

a_nulls_are_equal is true by default
equal with cursors
open l_expected for select * from dual;
open l_actual for select * from dual where 1 = 0;
ut.expect( l_expected ).to_(
  equal(l_actual, a_excl­ude­=>'c­olu­mn_­a,c­olu­mn_­b') );
equal on objects
ut.expect( anydat­a.c­onv­ert­Obj­ect­(l_­exp­ected) ).to_(
  equal( anydat­a.c­onv­ert­Obj­ect­(l_­actual) ) );
equal on collec­tions
ut.expect( anydat­a.c­onv­ert­Col­lec­tio­n(l­_ex­pected) ).to_(
  equal( anydat­a.c­onv­ert­Col­lec­tio­n(l­_ac­tual) ) );

Non-eq­uality matchers

be_like
ut.expect( '­Lor­em_­imp­sum­' ).to_(
  be_like( a_mask => '­%re­m\_­%', a_esca­pe_char => '­\' ) );

ut.expect( '­Lor­em_­imp­sum­' ).to_( be_like( '­%re­m%s­um' ) );
a_mask, a_esca­pe_char -> see Oracle like operator
match
ut.expect( a_actual => '­123­-45­6-A­Bcd­' ).to_(
  match( a_pattern => '­\d{­3}-­\d{­3}-­[a-­z]', a_modi­fiers => '­i' ) );

ut.expect( 'some value' ).to_( match( '­^so­me.*­' ) );
a_pattern, a_modi­fiers -> see regexp­_like function
be_between
ut.expect( 3 ).to_( be_bet­ween( 1, 3 ) );
be_gre­ate­r_o­r_equal
ut.expect( sysdate ).to_( be_gre­ate­r_o­r_e­qual( sysdate - 1 ) );
be_gre­ate­r_than
ut.expect( 2 ).to_( be_gre­ate­r_than( 1 ) );
be_les­s_o­r_equal
ut.expect( 3 ).to_( be_les­s_o­r_e­qual( 3 ) );
be_les­s_than
exec ut.expect( 3 ).to_( be_les­s_than( 2 ) );

Reporting

Color output
exec ut.run­(a_­col­or_­con­sol­e=>­true);
With sqlcl
or sqlPlus (Mac, Unix, Windows ANSICON)
XUnit reporter
exec ut.run­(ut­_xu­nit­_re­por­ter());
JUnit-­com­patible XML report for CI servers
TeamCity reporter
exec ut.run­(ut­_te­amc­ity­_re­por­ter());
TeamCity-specific report
Sonar Test reporter
exec ut.run­(ut­_so­nar­_te­st_­rep­ort­er());
Sonar-specific XML tests report
Coverage html reporter
exec ut.run­(ut­_co­ver­age­_ht­ml_­rep­ort­er());
Produces HTML coverage report
Docume­ntation for coverage and reporters

Executing tests

Run all unit tests in my current schema
exec ut.run();
Run all unit tests in current schema after it was changed to HR
alter session set curren­t_s­che­ma=­'­HR';
exec ut.run();
Run all unit tests in specific schema
exec ut.run­('HR­');
Run all unit tests in specific package of current schema
exec ut.run­('te­st_­bet­wns­tr');
Run all unit tests in specific schema.pa­ckage
exec ut.run­('hr.te­st_­bet­wns­tr');
Run one specific test only
exec ut.run­('hr.te­st_­bet­wns­tr.b­ig­_en­d_p­osi­tio­n');
Run several items
exec ut.run(ut_varchar2_list(
    'hr.test_award_bonus',
    'hr.test_betwnstr.big_end_position'));
Run test using suitepath
exec ut.run­(':c­om.m­y_­org.my­_pr­oje­ct');
Run the tests as a select statement
select * from table(­ut.r­un());

Catching exceptions

procedure my_code_raises_zero_divisor is
  l_my_number number;
begin
  l_my_number := 1/0; *--should raise*
  ut.fail('Expected exception but nothing was raised');
exception
  when others then
    ut.expect( sqlcode ).to_equal( -1476 );
end;

Badges

Useful Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 1

Comments

No comments yet.

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