8000 Merge pull request #1092 from utPLSQL/feature/sample_reporters · utPLSQL/utPLSQL@ed87327 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed87327

Browse files
authored
Merge pull request #1092 from utPLSQL/feature/sample_reporters
Added example of a reporter that reports out all expectation outputs,…
2 parents bbc8e9a + 959c1c9 commit ed87327

17 files changed

+265
-88
lines changed

examples/RunAllExamplesAsTests.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ whenever oserror exit failure rollback
33

44
@@RunAllExamples.sql
55

6+
prompt *******************************************************************************
7+
prompt All examples completed successfully
8+
prompt *******************************************************************************
9+
prompt
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prompt *******************************************************************************
2+
prompt Running custom reporters examples
3+
prompt *******************************************************************************
4+
Clear Screen
5+
set echo off
6+
set feedback on
7+
set linesize 1000
8+
9+
@@custom_reporters/run_ut_custom_reporter.sql
10+
@@custom_reporters/run_ut_expectations_reporter.sql

examples/RunDeveloperExamples.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ set linesize 1000
66

77
exec ut_ansiconsole_helper.color_enabled(true);
88
--developer examples
9-
prompt RunExampleTestSuiteWithCustomReporter
10-
@@developer_examples/RunExampleTestSuiteWithCustomReporter.sql
119
prompt RunExampleTestAnnotationsParsingTimeHugePackage
1210
@@developer_examples/RunExampleTestAnnotationsParsingTimeHugePackage.sql
1311
prompt RunExampleTestSuite

examples/between_string/run_betwnstr_test.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@@betwnstr.sql
2-
@@test_betwnstr.pkg
2+
@@test_betwnstr.pks
3+
@@test_betwnstr.pkb
34

45
set serveroutput on size unlimited format truncated
56

examples/between_string/run_betwnstr_test_coverage.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ set pagesize 0
44
set long 200000000
55
set longchunksize 1000000
66
@@betwnstr.sql
7-
@@test_betwnstr.pkg
7+
@@test_betwnstr.pks
8+
@@test_betwnstr.pkb
89

910
set serveroutput on size unlimited format truncated
1011

examples/between_string/test_betwnstr.pkg renamed to examples/between_string/test_betwnstr.pkb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
create or replace package test_betwnstr as
2-
3-
-- %suite(Between string function)
4-
5-
-- %test(Returns substring from start position to end position)
6-
procedure normal_case;
7-
8-
-- %test(Returns substring when start position is zero)
9-
procedure zero_start_position;
10-
11-
-- %test(Returns string until end if end position is greater than string length)
12-
procedure big_end_position;
13-
14-
-- %test(Returns null for null input string value)
15-
procedure null_string;
16-
17-
-- %test(Demo of a disabled test)
18-
-- %disabled
19-
procedure disabled_test;
20-
21-
end;
22-
/
231
create or replace package body test_betwnstr as
242

253
procedure normal_case is
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
create or replace package test_betwnstr as
2+
3+
-- %suite(Between string function)
4+
5+
-- %test(Returns substring from start position to end position)
6+
procedure normal_case;
7+
8+
-- %test(Returns substring when start position is zero)
9+
procedure zero_start_position;
10+
11+
-- %test(Returns string until end if end position is greater than string length)
12+
procedure big_end_position;
13+
14+
-- %test(Returns null for null input string value)
15+
procedure null_string;
16+
17+
-- %test(Demo of a disabled test)
18+
-- %disabled
19+
procedure disabled_test;
20+
21+
end;
22+
/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
prompt *******************************************************************************
2+
prompt Runnign tests with UT_CUSTOM_REPORTER on top of UT_DOCUMENTATION_REPROTER
3+
prompt *******************************************************************************
4+
5+
set echo off
6+
--install the example unit test packages
7+
@demo_of_expectations/demo_equal_matcher.sql
8+
@@ut_custom_reporter.tps
9+
@@ut_custom_reporter.tpb
10+
11+
set serveroutput on size unlimited format truncated
12+
13+
exec ut.run('demo_equal_matcher', ut_custom_reporter());
14+
15+
@demo_of_expectations/drop_demo_equal_matcher.sql
16+
drop type ut_custom_reporter;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
prompt *******************************************************************************
2+
prompt Runnign tests with UT_CUSTOM_REPORTER on top of UT_DOCUMENTATION_REPROTER
3+
prompt *******************************************************************************
4+
5+
set echo off
6+
--install the example unit test packages
7+
@demo_of_expectations/demo_equal_matcher.sql
8+
@@ut_expectations_reporter.tps
9+
@@ut_expectations_reporter.tpb
10+
11+
set serveroutput on size unlimited format truncated
12+
13+
exec ut.run('demo_equal_matcher', ut_expectations_reporter());
14+
15+
@demo_of_expectations/drop_demo_equal_matcher.sql
16+
drop type ut_expectations_reporter;

examples/developer_examples/ut_custom_reporter.tps renamed to examples/custom_reporters/ut_custom_reporter.tps

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ create or replace type ut_custom_reporter under ut_documentation_reporter
44

55
-- Member functions and procedures
66
constructor function ut_custom_reporter(a_tab_size integer default 4) return self as result,
7+
8+
/* The reporter is using base functions of parent type ( UT_DOCUMENTATION_REPORTER )
9+
It is altering the behavior of the base functions by change of the indentation.
10+
So the custom reporter is same as documentation reporter except that the tab size is bigger.
11+
Additionally, the reporter constructor accepts parameter to indicate the indentation size
12+
*/
713
overriding member function tab(self in ut_custom_reporter) return varchar2,
814
overriding member procedure print_text(a_text varchar2, a_item_type varchar2 := null),
915
overriding member procedure before_calling_suite(self in out nocopy ut_custom_reporter, a_suite ut_logical_suite),
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
create or replace type body ut_expectations_reporter is
2+
/*
3+
utPLSQL - Version 3
4+
Copyright 2016 - 2020 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
constructor function ut_expectations_reporter(a_report_all_expectations varchar2 := 'Y')return self as result is
20+
begin
21+
self.init($$plsql_unit);
22+
self.lvl := 0;
23+
self.report_all_expectations := substr(a_report_all_expectations,1,1);
24+
self.failed_test_running_count := 0;
25+
return;
26+
end;
27+
28+
/* The reporter procedure after_calling_test from ut_documentation_reporter is overriden here so that:
29+
- the test name is printed
30+
- the test staus is printed
31+
- test duration is printed
32+
- all expectation results from the test are printed (default) or only the failing ones
33+
- error stack trace is printed
34+
- dbms_output from test run is always printed
35+
*/
36+
overriding member procedure after_calling_test(a_test ut_test) as
37+
l_message varchar2(4000);
38+
39+
procedure print_expectation(a_expectation ut_expectation_result) is
40+
l_lines ut_varchar2_list;
41+
l_failed boolean := a_expectation.status > ut_utils.gc_success;
42+
begin
43+
if l_failed or self.report_all_expectations = 'Y' then
44+
l_lines := a_expectation.get_result_lines();
45+
for i in 1 .. l_lines.count loop
46+
if l_failed then
47+
self.print_red_text(l_lines(i));
48+
else
49+
self.print_green_text(l_lines(i));
50+
end if;
51+
end loop;
52+
self.print_cyan_text(a_expectation.caller_info);
53+
self.print_text(' ');
54+
end if;
55+
end;
56+
57+
procedure print_results_for_test(a_test ut_test) is
58+
begin
59+
self.lvl := self.lvl + 3;
60+
self.print_red_text(ut_utils.table_to_clob( a_test.get_error_stack_traces() ));
61+
for j in 1 .. a_test.all_expectations.count loop
62+
print_expectation(a_test.all_expectations(j));
63+
end loop;
64+
self.lvl := self.lvl - 3;
65+
end;
66+
begin
67+
l_message := coalesce(a_test.description, a_test.name)||' ['||round(a_test.execution_time,3)||' sec]';
68+
--if test failed, then add it to the failures list, print failure with number
69+
if a_test.result = ut_utils.gc_disabled then
70+
self.print_yellow_text(l_message || ' (DISABLED)');
71+
elsif a_test.result = ut_utils.gc_success then
72+
self.print_green_text(l_message);
73+
elsif a_test.result > ut_utils.gc_success then
74+
self.failed_test_running_count := self.failed_test_running_count + 1;
75+
self.print_red_text(l_message || ' (FAILED - ' || failed_test_running_count || ')');
76+
end if;
77+
78+
print_results_for_test(a_test);
79+
-- reproduce the output from before/after procedures and the test
80+
self.print_clob(a_test.get_serveroutputs);
81+
end;
82+
83+
overriding member procedure after_calling_run(a_run in ut_run) as
84+
l_summary_text varchar2(4000);
85+
l_warning_index pls_integer := 0;
86+
-- make all warning indexes uniformly indented
87+
c_warnings_lpad constant integer := length(to_char(a_run.results_count.warnings_count));
88+
89+
procedure print_item_warnings(a_item in ut_suite_item) is
90+
l_items ut_suite_items;
91+
begin
92+
if a_item is of (ut_logical_suite) then
93+
l_items := treat(a_item as ut_logical_suite).items;
94+
for i in 1 .. l_items.count loop
95+
print_item_warnings(l_items(i));
96+
end loop;
97+
end if;
98+
99+
if a_item.warnings is not null and a_item.warnings.count > 0 then
100+
for i in 1 .. a_item.warnings.count loop
101+
l_warning_index := l_warning_index + 1;
102+
self.print_text(' ' || lpad(l_warning_index, c_warnings_lpad) || ') ' || a_item.path);
103+
self.lvl := self.lvl + 3;
104+
self.print_red_text(a_item.warnings(i));
105+
self.lvl := self.lvl - 3;
106+
end loop;
107+
self.print_text(' ');
108+
end if;
109+
end;
110+
111+
procedure print_warnings(a_run in ut_run) is
112+
begin
113+
if a_run.results_count.warnings_count > 0 then
114+
self.print_text(' ');
115+
self.print_text('Warnings:');
116+
self.print_text(' ');
117+
for i in 1 .. a_run.items.count loop
118+
print_item_warnings(treat(a_run.items(i) as ut_suite_item));
119+
end loop;
120+
end if;
121+
end;
122+
123+
begin
124+
print_warnings(a_run);
125+
self.print_text('Finished in ' || a_run.execution_time || ' seconds');
126+
127+
l_summary_text :=
128+
a_run.results_count.total_count || ' tests, '
129+
|| a_run.results_count.failure_count || ' failed, ' || a_run.results_count.errored_count || ' errored, '
130+
|| a_run.results_count.disabled_count ||' disabled, ' || a_run.results_count.warnings_count || ' warning(s)';
131+
if a_run.results_count.failure_count + a_run.results_count.errored_count + a_run.results_count.warnings_count > 0 then
132+
self.print_red_text(l_summary_text);
133+
else
134+
self.print_green_text(l_summary_text);
135+
end if;
136+
if a_run.random_test_order_seed is not null then
137+
self.print_text('Tests were executed with random order seed '''||a_run.random_test_order_seed||'''.');
138+
end if;
139+
self.print_text(' ');
140+
(self as ut_reporter_base).after_calling_run(a_run);
141+
end;
142+
143+
overriding member function get_description return varchar2 as
144+
begin
145+
return 'A custom reporter for pretty-printing all expectation results directly under the test';
146+
end;
147+
148+
end;
149+
/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
create or replace type ut_expectations_reporter under ut_documentation_reporter(
2+
/*
3+
utPLSQL - Version 3
4+
Copyright 2016 - 2020 utPLSQL Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"):
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
report_all_expectations varchar2(1),
19+
20+
21+
constructor function ut_expectations_reporter(a_report_all_expectations varchar2 := 'Y') return self as result,
22+
23+
overriding member procedure after_calling_test(a_test ut_test),
24 B41A +
overriding member procedure after_calling_run(a_run in ut_run),
25+
26+
overriding member function get_description return varchar2
27+
28+
)
29+
not final
30+
/

examples/demo_of_expectations/demo_equal_matcher.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ create or replace package demo_equal_matcher as
1818
-- %suitepath(org.utplsql.v3.demo.matchers)
1919

2020
-- TODO this should go into context(compare_objects, Comparing objects)
21-
-- %context(compare_objects, Comparing objects)
21+
-- %context(Comparing objects)
2222

2323
-- %test
2424
-- %displayname(Gives success when comparing identical objects containing identical data)
@@ -48,7 +48,7 @@ create or replace package demo_equal_matcher as
4848
-- %displayname(Gives failure when comparing different objects containing identical data)
4949
procedure object_compare_different_type;
5050

51-
-- %end_context
51+
-- %endcontext
5252

5353
end;
5454
/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
drop package demo_equal_matcher;
2+
drop type demo_departments;
3+
drop type demo_department_new;
4+
drop type demo_department;
5+

examples/demo_of_expectations/run.sql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ set serveroutput on size unlimited format truncated
44

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

7-
drop package demo_equal_matcher;
8-
drop type demo_departments;
9-
drop type demo_department_new;
10-
drop type demo_department;
7+
@@drop_demo_equal_matcher.sql
118

0 commit comments

Comments
 (0)
0