|
| 1 | +create or replace type body ut_tfs_junit_reporter is |
| 2 | + /* |
| 3 | + utPLSQL - Version 3 |
| 4 | + Copyright 2016 - 2017 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_tfs_junit_reporter(self in out nocopy ut_tfs_junit_reporter) return self as result is |
| 20 | + begin |
| 21 | + self.init($$plsql_unit); |
| 22 | + return; |
| 23 | + end; |
| 24 | + |
| 25 | + overriding member procedure after_calling_run(self in out nocopy ut_tfs_junit_reporter, a_run in ut_run) is |
| 26 | + begin |
| 27 | + junit_version_one(a_run); |
| 28 | + end; |
| 29 | + |
| 30 | + member procedure junit_version_one(self in out nocopy ut_tfs_junit_reporter,a_run in ut_run) is |
| 31 | + l_suite_id integer := 0; |
| 32 | + l_tests_count integer := a_run.results_count.disabled_count + a_run.results_count.success_count + |
| 33 | + a_run.results_count.failure_count + a_run.results_count.errored_count; |
| 34 | + |
| 35 | + function get_common_suite_attributes(a_item ut_suite_item) return varchar2 is |
| 36 | + begin |
| 37 | + return ' errors="' ||a_item.results_count.errored_count || '"' || |
| 38 | + ' failures="' || a_item.results_count.failure_count || |
| 39 | + '" name="' || dbms_xmlgen.convert(nvl(a_item.description, a_item.name)) || '"' || |
| 40 | + ' time="' || ut_utils.to_xml_number_format(a_item.execution_time()) || '" '|| |
| 41 | + ' timestamp="' || to_char(sysdate,'RRRR-MM-DD"T"HH24:MI:SS') || '" '|| |
| 42 | + ' hostname="' || sys_context('USERENV','HOST') || '" '; |
| 43 | + end; |
| 44 | + |
| 45 | + function get_common_testcase_attributes(a_item ut_suite_item) return varchar2 is |
| 46 | + begin |
| 47 | + return ' name="' || dbms_xmlgen.convert(nvl(a_item.description, a_item.name)) || '"' || |
| 48 | + ' time="' || ut_utils.to_xml_number_format(a_item.execution_time()) || '"'; |
| 49 | + end; |
| 50 | + |
| 51 | + function get_path(a_path_with_name varchar2, a_name varchar2) return varchar2 is |
| 52 | + begin |
| 53 | + return regexp_substr(a_path_with_name, '(.*)\.' ||a_name||'$',subexpression=>1); |
| 54 | + end; |
| 55 | + |
| 56 | + procedure print_test_results(a_test ut_test) is |
| 57 | + l_lines ut_varchar2_list; |
| 58 | + l_output clob; |
| 59 | + begin |
| 60 | + self.print_text('<testcase classname="' || dbms_xmlgen.convert(get_path(a_test.path, a_test.name)) || '" ' || |
| 61 | + get_common_testcase_attributes(a_test) || '>'); |
| 62 | + /* |
| 63 | + According to specs : |
| 64 | + - A failure is a test which the code has explicitly failed by using the mechanisms for that purpose. |
| 65 | + e.g., via an assertEquals |
| 66 | + - An errored test is one that had an unanticipated problem. |
| 67 | + e.g., an unchecked throwable; or a problem with the implementation of the test. |
| 68 | + */ |
| 69 | + |
| 70 | + if a_test.result = ut_utils.tr_error then |
| 71 | + self.print_text('<error type="error" message="Error while executing '||a_test.name||'">'); |
| 72 | + self.print_text('<![CDATA['); |
| 73 | + self.print_clob(ut_utils.table_to_clob(a_test.get_error_stack_traces())); |
| 74 | + self.print_text(']]>'); |
| 75 | + self.print_text('</error>'); |
| 76 | + -- Do not count error as failure |
| 77 | + elsif a_test.result = ut_utils.tr_failure then |
| 78 | + self.print_text('<failure type="failure" message="Test '||a_test.name||' failed">'); |
| 79 | + self.print_text('<![CDATA['); |
| 80 | + for i in 1 .. a_test.failed_expectations.count loop |
| 81 | + l_lines := a_test.failed_expectations(i).get_result_lines(); |
| 82 | + for j in 1 .. l_lines.count loop |
| 83 | + self.print_text(l_lines(j)); |
| 84 | + end loop; |
| 85 | + self.print_text(a_test.failed_expectations(i).caller_info); |
| 86 | + end loop; |
| 87 | + self.print_text(']]>'); |
| 88 | + self.print_text('</failure>'); |
| 89 | + end if; |
| 90 | + |
| 91 | + self.print_text('</testcase>'); |
| 92 | + end; |
| 93 | + |
| 94 | + procedure print_suite_results(a_suite ut_logical_suite, a_suite_id in out nocopy integer) is |
| 95 | + l_tests_count integer := a_suite.results_count.disabled_count + a_suite.results_count.success_count + |
| 96 | + a_suite.results_count.failure_count + a_suite.results_count.errored_count; |
| 97 | + l_suite ut_suite; |
| 98 | + begin |
| 99 | + |
| 100 | + for i in 1 .. a_suite.items.count loop |
| 101 | + if a_suite.items(i) is of(ut_logical_suite) then |
| 102 | + print_suite_results(treat(a_suite.items(i) as ut_logical_suite), a_suite_id); |
| 103 | + end if; |
| 104 | + end loop; |
| 105 | + |
| 106 | + if a_suite is of(ut_suite) then |
| 107 | + a_suite_id := a_suite_id + 1; |
| 108 | + self.print_text('<testsuite tests="' || l_tests_count || '"' || ' id="' || a_suite_id || '"' || ' package="' || |
| 109 | + dbms_xmlgen.convert(a_suite.path) || '" ' || get_common_suite_attributes(a_suite) || '>'); |
| 110 | + self.print_text('<properties/>'); |
| 111 | + for i in 1 .. a_suite.items.count loop |
| 112 | + if a_suite.items(i) is of(ut_test) then |
| 113 | + print_test_results(treat(a_suite.items(i) as ut_test)); |
| 114 | + end if; |
| 115 | + end loop; |
| 116 | + l_suite := treat(a_suite as ut_suite); |
| 117 | + if l_suite.before_all.serveroutput is not null or l_suite.after_all.serveroutput is not null then |
| 118 | + self.print_text('<system-out>'); |
| 119 | + self.print_text('<![CDATA['); |
| 120 | + self.print_clob(l_suite.get_serveroutputs()); |
| 121 | + self.print_text(']]>'); |
| 122 | + self.print_text('</system-out>'); |
| 123 | + else |
| 124 | + self.print_text('<system-out/>'); |
| 125 | + end if; |
| 126 | + |
| 127 | + if l_suite.before_all.error_stack is not null or l_suite.after_all.error_stack is not null then |
| 128 | + self.print_text('<system-err>'); |
| 129 | + self.print_text('<![CDATA['); |
| 130 | + self.print_text(trim(l_suite.before_all.error_stack) || trim(chr(10) || chr(10) || l_suite.after_all.error_stack)); |
| 131 | + self.print_text(']]>'); |
| 132 | + self.print_text('</system-err>'); |
| 133 | + else |
| 134 | + self.print_text('<system-err/>'); |
| 135 | + end if; |
| 136 | + self.print_text('</testsuite>'); |
| 137 | + end if; |
| 138 | + end; |
| 139 | + |
| 140 | + begin |
| 141 | + l_suite_id := 0; |
| 142 | + self.print_text('<testsuites>'); |
| 143 | + for i in 1 .. a_run.items.count loop |
| 144 | + print_suite_results(treat(a_run.items(i) as ut_logical_suite), l_suite_id); |
| 145 | + end loop; |
| 146 | + self.print_text('</testsuites>'); |
| 147 | + end; |
| 148 | + |
| 149 | + overriding member function get_description return varchar2 as |
| 150 | + begin |
| 151 | + return 'Provides outcomes in a format conforming with JUnit version for TFS / VSTS. |
| 152 | + As defined by specs :https://docs.microsoft.com/en-us/vsts/build-release/tasks/test/publish-test-results?view=vsts |
| 153 | + Version is based on windy road junit https://github.com/windyroad/JUnit-Schema/blob/master/JUnit.xsd.'; |
| 154 | + end; |
| 155 | + |
| 156 | +end; |
| 157 | +/ |
0 commit comments