8000 Added example of a reporter that reports out all expectation outputs,… by jgebal · Pull Request #1092 · utPLSQL/utPLSQL · GitHub
[go: up one dir, main page]

Skip to content

Added example of a reporter that reports out all expecta 8000 tion outputs,… #1092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/RunAllExamplesAsTests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ whenever oserror exit failure rollback

@@RunAllExamples.sql

prompt *******************************************************************************
prompt All examples completed successfully
prompt *******************************************************************************
prompt
10 changes: 10 additions & 0 deletions examples/RunCustomReportersExamples.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prompt *******************************************************************************
prompt Running custom reporters examples
prompt *******************************************************************************
Clear Screen
set echo off
set feedback on
set linesize 1000

@@custom_reporters/run_ut_custom_reporter.sql
@@custom_reporters/run_ut_expectations_reporter.sql
2 changes: 0 additions & 2 deletions examples/RunDeveloperExamples.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ set linesize 1000

exec ut_ansiconsole_helper.color_enabled(true);
--developer examples
prompt RunExampleTestSuiteWithCustomReporter
@@developer_examples/RunExampleTestSuiteWithCustomReporter.sql
prompt RunExampleTestAnnotationsParsingTimeHugePackage
@@developer_examples/RunExampleTestAnnotationsParsingTimeHugePackage.sql
prompt RunExampleTestSuite
Expand Down
3 changes: 2 additions & 1 deletion examples/between_string/run_betwnstr_test.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@@betwnstr.sql
@@test_betwnstr.pkg
@@test_betwnstr.pks
@@test_betwnstr.pkb

set serveroutput on size unlimited format truncated

Expand Down
3 changes: 2 additions & 1 deletion examples/between_string/run_betwnstr_test_coverage.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ set pagesize 0
set long 200000000
set longchunksize 1000000
@@betwnstr.sql
@@test_betwnstr.pkg
@@test_betwnstr.pks
@@test_betwnstr.pkb

set serveroutput on size unlimited format truncated

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
create or replace package test_betwnstr as

-- %suite(Between string function)

-- %test(Returns substring from start position to end position)
procedure normal_case;

-- %test(Returns substring when start position is zero)
procedure zero_start_position;

-- %test(Returns string until end if end position is greater than string length)
procedure big_end_position;

-- %test(Returns null for null input string value)
procedure null_string;

-- %test(Demo of a disabled test)
-- %disabled
procedure disabled_test;

end;
/
create or replace package body test_betwnstr as

procedure normal_case is
Expand Down
22 changes: 22 additions & 0 deletions examples/between_string/test_betwnstr.pks
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
create or replace package test_betwnstr as

-- %suite(Between string function)

-- %test(Returns substring from start position to end position)
procedure normal_case;

-- %test(Returns substring when start position is zero)
procedure zero_start_position;

-- %test(Returns string until end if end position is greater than string length)
procedure big_end_position;

-- %test(Returns null for null input string value)
procedure null_string;

-- %test(Demo of a disabled test)
-- %disabled
procedure disabled_test;

end;
/
16 changes: 16 additions & 0 deletions examples/custom_reporters/run_ut_custom_reporter.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
prompt *******************************************************************************
prompt Runnign tests with UT_CUSTOM_REPORTER on top of UT_DOCUMENTATION_REPROTER
prompt *******************************************************************************

set echo off
--install the example unit test packages
@demo_of_expectations/demo_equal_matcher.sql
@@ut_custom_reporter.tps
@@ut_custom_reporter.tpb

set serveroutput on size unlimited format truncated

exec ut.run('demo_equal_matcher', ut_custom_reporter());

@demo_of_expectations/drop_demo_equal_matcher.sql
drop type ut_custom_reporter;
16 changes: 16 additions & 0 deletions examples/custom_reporters/run_ut_expectations_reporter.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
prompt *******************************************************************************
prompt Runnign tests with UT_CUSTOM_REPORTER on top of UT_DOCUMENTATION_REPROTER
prompt *******************************************************************************

set echo off
--install the example unit test packages
@demo_of_expectations/demo_equal_matcher.sql
@@ut_expectations_reporter.tps
@@ut_expectations_reporter.tpb

set serveroutput on size unlimited format truncated

exec ut.run('demo_equal_matcher', ut_expectations_reporter());

@demo_of_expectations/drop_demo_equal_matcher.sql
drop type ut_expectations_reporter;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ create or replace type ut_custom_reporter under ut_documentation_reporter

-- Member functions and procedures
constructor function ut_custom_reporter(a_tab_size integer default 4) return self as result,

/* The reporter is using base functions of parent type ( UT_DOCUMENTATION_REPORTER )
It is altering the behavior of the base functions by change of the indentation.
So the custom reporter is same as documentation reporter except that the tab size is bigger.
Additionally, the reporter constructor accepts parameter to indicate the indentation size
*/
overriding member function tab(self in ut_custom_reporter) return varchar2,
overriding member procedure print_text(a_text varchar2, a_item_type varchar2 := null),
overriding member procedure before_calling_suite(self in out nocopy ut_custom_reporter, a_suite ut_logical_suite),
Expand Down
149 changes: 149 additions & 0 deletions examples/custom_reporters/ut_expectations_reporter.tpb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
create or replace type body ut_expectations_reporter is
/*
utPLSQL - Version 3
Copyright 2016 - 2020 utPLSQL Project

Licensed under the Apache License, Version 2.0 (the "License"):
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

constructor function ut_expectations_reporter(a_report_all_expectations varchar2 := 'Y')return self as result is
begin
self.init($$plsql_unit);
self.lvl := 0;
self.report_all_expectations := substr(a_report_all_expectations,1,1);
self.failed_test_running_count := 0;
return;
end;

/* The reporter procedure after_calling_test from ut_documentation_reporter is overriden here so that:
- the test name is printed
- the test staus is printed
- test duration is printed
- all expectation results from the test are printed (default) or only the failing ones
- error stack trace is printed
- dbms_output from test run is always printed
*/
overriding member procedure after_calling_test(a_test ut_test) as
l_message varchar2(4000);

procedure print_expectation(a_expectation ut_expectation_result) is
l_lines ut_varchar2_list;
l_failed boolean := a_expectation.status > ut_utils.gc_success;
begin
if l_failed or self.report_all_expectations = 'Y' then
l_lines := a_expectation.get_result_lines();
for i in 1 .. l_lines.count loop
if l_failed then
self.print_red_text(l_lines(i));
else
self.print_green_text(l_lines(i));
end if;
end loop;
self.print_cyan_text(a_expectation.caller_info);
self.print_text(' ');
end if;
end;

procedure print_results_for_test(a_test ut_test) is
begin
self.lvl := self.lvl + 3;
self.print_red_text(ut_utils.table_to_clob( a_test.get_error_stack_traces() ));
for j in 1 .. a_test.all_expectations.count loop
print_expectation(a_test.all_expectations(j));
end loop;
self.lvl := self.lvl - 3;
end;
begin
l_message := coalesce(a_test.description, a_test.name)||' ['||round(a_test.execution_time,3)||' sec]';
--if test failed, then add it to the failures list, print failure with number
if a_test.result = ut_utils.gc_disabled then
self.print_yellow_text(l_message || ' (DISABLED)');
elsif a_test.result = ut_utils.gc_success then
self.print_green_text(l_message);
elsif a_test.result > ut_utils.gc_success then
self.failed_test_running_count := self.failed_test_running_count + 1;
self.print_red_text(l_message || ' (FAILED - ' || failed_test_running_count || ')');
end if;

print_results_for_test(a_test);
-- reproduce the output from before/after procedures and the test
self.print_clob(a_test.get_serveroutputs);
end;

overriding member procedure after_calling_run(a_run in ut_run) as
l_summary_text varchar2(4000);
l_warning_index pls_integer := 0;
-- make all warning indexes uniformly indented
c_warnings_lpad constant integer := length(to_char(a_run.results_count.warnings_count));

procedure print_item_warnings(a_item in ut_suite_item) is
l_items ut_suite_items;
begin
if a_item is of (ut_logical_suite) then
l_items := treat(a_item as ut_logical_suite).items;
for i in 1 .. l_items.count loop
print_item_warnings(l_items(i));
end loop;
end if;

if a_item.warnings is not null and a_item.warnings.count > 0 then
for i in 1 .. a_item.warnings.count loop
l_warning_index := l_warning_index + 1;
self.print_text(' ' || lpad(l_warning_index, c_warnings_lpad) || ') ' || a_item.path);
self.lvl := self.lvl + 3;
self.print_red_text(a_item.warnings(i));
self.lvl := self.lvl - 3;
end loop;
self.print_text(' ');
end if;
end;

procedure print_warnings(a_run in ut_run) is
begin
if a_run.results_count.warnings_count > 0 then
self.print_text(' ');
self.print_text('Warnings:');
self.print_text(' ');
for i in 1 .. a_run.items.count loop
print_item_warnings(treat(a_run.items(i) as ut_suite_item));
end loop;
end if;
end;

begin
print_warnings(a_run);
self.print_text('Finished in ' || a_run.execution_time || ' seconds');

l_summary_text :=
a_run.results_count.total_count || ' tests, '
|| a_run.results_count.failure_count || ' failed, ' || a_run.results_count.errored_count || ' errored, '
|| a_run.results_count.disabled_count ||' disabled, ' || a_run.results_count.warnings_count || ' warning(s)';
if a_run.results_count.failure_count + a_run.results_count.errored_count + a_run.results_count.warnings_count > 0 then
self.print_red_text(l_summary_text);
else
self.print_green_text(l_summary_text);
end if;
if a_run.random_test_order_seed is not null then
self.print_text('Tests were executed with random order seed '''||a_run.random_test_order_seed||'''.');
end if;
self.print_text(' ');
(self as ut_reporter_base).after_calling_run(a_run);
end;

overriding member function get_description return varchar2 as
begin
return 'A custom reporter for pretty-printing all expectation results directly under the test';
end;

end;
/
30 changes: 30 additions & 0 deletions examples/custom_reporters/ut_expectations_reporter.tps
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
create or replace type ut_expectations_reporter under ut_documentation_reporter(
/*
utPLSQL - Version 3
Copyright 2016 - 2020 utPLSQL Project

Licensed under the Apache License, Version 2.0 (the "License"):
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
report_all_expectations varchar2(1),


constructor function ut_expectations_reporter(a_report_all_expectations varchar2 := 'Y') return self as result,

overriding member procedure after_calling_test(a_test ut_test),
overriding member procedure after_calling_run(a_run in ut_run),

overriding member function get_description return varchar2

)
not final
/
4 changes: 2 additions & 2 deletions examples/demo_of_expectations/demo_equal_matcher.sql
10662
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ create or replace package demo_equal_matcher as
-- %suitepath(org.utplsql.v3.demo.matchers)

-- TODO this should go into context(compare_objects, Comparing objects)
-- %context(compare_objects, Comparing objects)
-- %context(Comparing objects)

-- %test
-- %displayname(Gives success when comparing identical objects containing identical data)
Expand Down Expand Up @@ -48,7 +48,7 @@ create or replace package demo_equal_matcher as
-- %displayname(Gives failure when comparing different objects containing identical data)
procedure object_compare_different_type;

-- %end_context
-- %endcontext

end;
/
Expand Down
5 changes: 5 additions & 0 deletions examples/demo_of_expectations/drop_demo_equal_matcher.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
drop package demo_equal_matcher;
drop type demo_departments;
drop type demo_department_new;
drop type demo_department;

5 changes: 1 addition & 4 deletions examples/demo_of_expectations/run.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ set serveroutput on size unlimited format truncated

exec ut.run(user||'.demo_equal_matcher');

drop package demo_equal_matcher;
drop type demo_departments;
drop type demo_department_new;
drop type demo_department;
@@drop_demo_equal_matcher.sql

Loading
0