utPLSQLのインストール手順
% unzip -q utPLSQL.zip
% cd utPLSQL
utPLSQL % ls
CHANGELOG.md CONTRIBUTING.md examples readme.md VERSION
CODE_OF_CONDUCT.md docs LICENSE source
utPLSQL %
utPLSQL % cd source
source % ls
api create_utplsql_owner.sql install.sql
check_object_grants.sql define_ut3_owner_param.sql reporters
check_sys_grants.sql dummy.sql set_install_params.sql
core expectations uninstall_all.sql
create_grants.sql install_above_12_1.sql uninstall_coverage_tables.sql
create_synonyms_and_grants_for_public.sql install_component.sql uninstall_objects.sql
create_synonyms.sql install_ddl_trigger.sql uninstall_synonyms.sql
create_user_grants.sql install_headless_with_trigger.sql uninstall.sql
create_user_synonyms.sql install_headless.sql
source %
- DDLトリガーを作成せずに、一度にすべてインストールする
- DDLトリガーを作成して、一度にすべてインストールする
- ユーザーの作成など、ステップを分けてインストールする
SQL> @install_headless_with_trigger.sql ut3 3qgeqf4tq5r3t2 users
Creating utPLSQL user ut3
-------------------------------------------------------------
Installing utPLSQL v3 framework into ut3 schema
-------------------------------------------------------------
Switching current schema to ut3
-------------------------------------------------------------
Installing component UT_DBMS_OUTPUT_CACHE
エラーはありません。
--------------------------------------------------------------
Installing component UT_VARCHAR2_LIST
[中略]
Synonym DBMSPCC_RUNSは作成されました。
Synonym DBMSPCC_UNITSは作成されました。
Installing component UT_TRIGGER_ANNOTATION_PARSING
エラーはありません。
--------------------------------------------------------------
Oracle AI Database 26ai Free Release 23.26.1.0.0 - Develop, Learn, and Run for Free
Version 23.26.1.0.0から切断されました
source %
create or replace package test_run_sql as
--%suite(Test function run_sql)
--%beforeall
procedure prepare_data;
--%afterall
procedure cleanup_data;
--%test(apple)
procedure test_select_apple;
--%test(banana)
procedure test_select_banana;
end test_run_sql;
/
select text from dba_source where owner = 'APEXDEV' and name = 'TEST_RUN_SQL' and type = 'PACKAGE' order by line asc;
SQL> select text from dba_source where owner = 'APEXDEV' and name = 'TEST_RUN_SQL' and type = 'PACKAGE' order by line asc;
TEXT
______________________________________
package test_run_sql as
--%suite(Test function run_sql)
--%beforeall
procedure prepare_data;
--%afterall
procedure cleanup_data;
--%test(apple)
procedure test_select_apple;
--%test(banana)
procedure test_select_banana;
end test_run_sql;
15行が選択されました。
SQL>
パッケージ定義に付与するアノテーションについて
create or replace package test_run_sql as
--%suite(Test function run_sql)
--%beforeall
procedure prepare_data;
--%afterall
procedure cleanup_data;
--%test(apple)
procedure test_select_apple;
--%test(banana)
procedure test_select_banana;
end test_run_sql;
/
SQL> set serveroutput on
SQL> exec ut.run('test_run_sql');
Test function run_sql
apple [.001 sec]
banana [.002 sec] (FAILED - 1)
Failures:
1) test_select_banana
Actual: json was expected to equal: json
Diff: 1 differences found
1 unequal values
Actual value: "apple" was expected to be: "banana" on path: $[0]."VALUE"
at "APEXDEV.TEST_RUN_SQL.TEST_SELECT_BANANA", line 41 ut.expect(l_actual).to_( equal(l_expect) );
Finished in .004732 seconds
2 tests, 1 failed, 0 errored, 0 disabled, 0 warning(s)
PL/SQLプロシージャが正常に完了しました。
SQL>
- アノテーションbeforewallが指定されたプロシージャprepare_dataが最初に実行されます。
- アノテーションtest(apple)が指定されたプロシージャtest_select_appleが単体テストとして実行されます。テスト名はappleです。
- アノテーションtest(banana)が指定されたプロシージャtest_select_bananaが単体テストとして実行されます。テスト名はbananaです。
- アノテーションafterallが指定されたプロシージャcleanup_dataが最後に実行されます。
結果を検証するファンクションについて
単体テストの結果の検証に、Expectationsを使用します。
ut.expect( a_actual {data-type} [, a_message {varchar2}] ).to_( {matcher} );
ut.expect( a_actual {data-type} [, a_message {varchar2}] ).not_to( {matcher} );
これは以下のように短縮できます。
ut.expect(l_actual).to_equal(l_expect);
単体テストを記述するパッケージ本体は、プロシージャのコードにut.expectの呼び出しを含めます。
create or replace package body test_run_sql as
procedure prepare_data
as
begin
insert into my_test(value) values('apple');
end prepare_data;
procedure cleanup_data
as
begin
delete from my_test where value = 'apple';
end cleanup_data;
procedure test_select_apple
as
l_sql clob;
l_expect json_array_t;
l_result clob;
l_actual json_array_t;
begin
l_sql := q'[select value from my_test where value = 'apple']';
l_expect := json_array_t.parse('[{"VALUE":"apple"}]');
l_result := my_run_sql(l_sql);
l_actual := json_array_t.parse(l_result);
ut.expect(l_actual).to_equal(l_expect);
end test_select_apple;
procedure test_select_banana
as
l_sql clob;
l_expect json_array_t;
l_result clob;
l_actual json_array_t;
begin
l_sql := q'[select value from my_test where value = 'apple']';
l_expect := json_array_t.parse('[{"VALUE":"banana"}]');
l_result := my_run_sql(l_sql);
l_actual := json_array_t.parse(l_result);
-- ut.expect(l_actual).to_equal(l_expect);
ut.expect(l_actual).to_( equal(l_expect) );
end test_select_banana;
end test_run_sql;
/
単体テストの実行手順
それぞれのテストをパッケージとして実装して、単体テストを実行します。テストの実行には、ut.runまたはut_runner.runを呼び出します。
SQL> exec ut.run('test_run_sql.test_select_apple');
Test function run_sql
apple [.002 sec]
Finished in .003497 seconds
1 tests, 0 failed, 0 errored, 0 disabled, 0 warning(s)
PL/SQLプロシージャが正常に完了しました。
SQL>
SQL> exec ut.run('test_run_sql.test_select_*');
Test function run_sql
apple [.001 sec]
banana [.002 sec] (FAILED - 1)
Failures:
1) test_select_banana
Actual: json was expected to equal: json
Diff: 1 differences found
1 unequal values
Actual value: "apple" was expected to be: "banana" on path: $[0]."VALUE"
at "APEXDEV.TEST_RUN_SQL.TEST_SELECT_BANANA", line 41 ut.expect(l_actual).to_( equal(l_expect) );
Finished in .004282 seconds
2 tests, 1 failed, 0 errored, 0 disabled, 0 warning(s)
PL/SQLプロシージャが正常に完了しました。
SQL>
テスト結果の出力方法
exec ut.run('test_run_sql.test_select_*', ut_documentation_reporter());
SQL> exec ut.run('test_run_sql.test_select_*', ut_documentation_reporter());
Test function run_sql
apple [.001 sec]
banana [.002 sec] (FAILED - 1)
Failures:
1) test_select_banana
Actual: json was expected to equal: json
Diff: 1 differences found
1 unequal values
Actual value: "apple" was expected to be: "banana" on path: $[0]."VALUE"
at "APEXDEV.TEST_RUN_SQL.TEST_SELECT_BANANA", line 41 ut.expect(l_actual).to_( equal(l_expect) );
Finished in .004351 seconds
2 tests, 1 failed, 0 errored, 0 disabled, 0 warning(s)
PL/SQLプロシージャが正常に完了しました。
SQL>
Reporterのひとつに、JUnit Reporterがあります。
exec ut.run('test_run_sql.test_select_*', ut_junit_reporter());
SQL> exec ut.run('test_run_sql.test_select_*', ut_junit_reporter());
<?xml version="1.0"?>
<testsuites tests="2" disabled="0" errors="0" failures="1" name="" time=".004432" >
<testsuite tests="2" id="1" package="test_run_sql" disabled="0" errors="0" failures="1" name="Test function run_sql" time=".004356" >
<testcase classname="test_run_sql" assertions="1" name="apple" time=".001344" >
<system-out/>
<system-err/>
</testcase>
<testcase classname="test_run_sql" assertions="1" name="banana" time=".002119" status="Failure">
<failure>
<![CDATA[
Actual: json was expected to equal: json
Diff: 1 differences found
1 unequal values
Actual value: "apple" was expected to be: "banana" on path: $[0]."VALUE"
at "APEXDEV.TEST_RUN_SQL.TEST_SELECT_BANANA", line 41 ut.expect(l_actual).to_( equal(l_expect) );
]]>
</failure>
<system-out/>
<system-err/>
</testcase>
<system-out/>
<system-err/>
</testsuite>
</testsuites>
PL/SQLプロシージャが正常に完了しました。
SQL>
begin
for r in (
select column_value from ut.run('test_run_sql.test_select_*', ut_junit_reporter())
)
loop
dbms_output.put_line(r.column_value);
end loop;
end;
/
SQL> begin
2 for r in (
3 select column_value from ut.run('test_run_sql.test_select_*', ut_junit_reporter())
4 )
5 loop
6 dbms_output.put_line(r.column_value);
7 end loop;
8 end;
9* /
<?xml version="1.0"?>
<testsuites tests="2" disabled="0" errors="0" failures="1" name="" time=".004181" >
<testsuite tests="2" id="1" package="test_run_sql" disabled="0" errors="0" failures="1" name="Test function run_sql" time=".004112" >
<testcase classname="test_run_sql" assertions="1" name="apple" time=".001175" >
<system-out/>
<system-err/>
</testcase>
<testcase classname="test_run_sql" assertions="1" name="banana" time=".002171" status="Failure">
<failure>
<![CDATA[
Actual: json was expected to equal: json
Diff: 1 differences found
1 unequal values
Actual value: "apple" was expected to be: "banana" on path: $[0]."VALUE"
at "APEXDEV.TEST_RUN_SQL.TEST_SELECT_BANANA", line 41 ut.expect(l_actual).to_( equal(l_expect) );
]]>
</failure>
<system-out/>
<system-err/>
</testcase>
<system-out/>
<system-err/>
</testsuite>
</testsuites>
PL/SQLプロシージャが正常に完了しました。
SQL>