SASUnit Examples  Version 1.5.0
_reportjunitxml.sas
Go to the documentation of this file.
1 
21 %MACRO _reportJUnitXML(o_file =
22  );
23 
27  %LOCAL _SU_Error;
28  %LET _SU_Error = 2; /* Result-Code for failed SASUnit-tests */
29 
30  PROC SQL NOPRINT;
31  /* Start with the test-scenarion information */
32  CREATE TABLE Work.Combined AS
33  SELECT DISTINCT
34  1 AS isScenario
35  , 0 AS failures FORMAT = best12.
36  , LEFT(PUT(scn_id,z3.)) AS id LENGTH = 7 FORMAT = $7.
37  , 0 AS cas_id
38  , scn_id AS scn_id
39  , scn_desc AS name
40  , scn_path AS classname
41  , scn_start AS timestamp FORMAT = e8601dt.
42  , (scn_end-scn_start) AS time
43  , 0 AS tests
44  FROM &d_rep.
45  ;
46 
47  UPDATE Work.Combined SET time = 0 WHERE time = .;
48  UPDATE Work.Combined SET tests = (SELECT COUNT(DISTINCT(cas_id)) FROM &d_rep. WHERE scn_id = Combined.scn_id);
49  UPDATE Work.Combined SET failures = (SELECT COUNT(*) FROM &d_rep. (where=(tst_res = &_SU_Error.)) WHERE scn_id = Combined.scn_id);
50 
51  /* Insert testcases */
52  INSERT INTO Work.Combined
53  SELECT DISTINCT
54  0 AS isScenario
55  , cas_res = &_SU_Error. AS failures FORMAT = best12.
56  , put (scn_id,z3.) !! "-" !! put(cas_id,z3.)
57  AS id LENGTH = 7 FORMAT = $7.
58  , cas_id AS cas_id
59  , scn_id AS scn_id
60  , cas_desc AS name
61  , cas_pgm AS classname
62  , cas_start AS timestamp FORMAT = e8601dt.
63  , (cas_end-cas_start) AS time
64  , 1 AS tests
65  FROM &d_rep
66  ;
67  QUIT;
68 
69  PROC SORT
70  DATA = Work.Combined
71  OUT = Work.Combined
72  ;
73  BY /* ASC */ scn_id
74  DESCENDING isScenario
75  /* ASC */ id
76  ;
77  RUN;
78 
79  /* Assert that the failed tests are listed first */
80  PROC SORT
81  DATA = &d_rep.
82  OUT = Work.Failures
83  ;
84  BY scn_id
85  cas_id
86  DESCENDING tst_res
87  tst_id
88  ;
89  RUN;
90 
91  /* Keep first observation per testcase, i.e. only the first error within a test will be reported */
92  DATA Work.Failures ;
93  SET Work.Failures;
94  BY scn_id
95  cas_id
96  DESCENDING tst_res
97  tst_id
98  ;
99  if( first.cas_id );
100  RUN;
101 
102  PROC SQL NOPRINT;
103  /* Combine testdata with error messages */
104  CREATE TABLE Work.Junit AS
105  SELECT C.*
106  , F.tst_errmsg AS message
107  , F.tst_type AS type
108  FROM Work.Combined AS C
109  LEFT JOIN Work.Failures AS F
110  ON ( C.SCN_ID = F.SCN_ID
111  AND C.CAS_ID = F.CAS_ID )
112  ;
113 
114  /* Replace special characters for XML */
115  UPDATE Work.Junit SET name = TRANWRD(name, '&', '&' );
116  UPDATE Work.Junit SET name = TRANWRD(name, '<', '&lt;' );
117  UPDATE Work.Junit SET name = TRANWRD(name, '>', '&gt;' );
118  UPDATE Work.Junit SET name = TRANWRD(name, '"', '&quot;');
119 
120  UPDATE Work.Junit SET message = TRANWRD(message, '&', '&amp;' );
121  UPDATE Work.Junit SET message = TRANWRD(message, '<', '&lt;' );
122  UPDATE Work.Junit SET message = TRANWRD(message, '>', '&gt;' );
123  UPDATE Work.Junit SET message = TRANWRD(message, '"', '&quot;');
124  UPDATE Work.Junit SET message = '&#160;' where message = "";
125 
126  UPDATE Work.Junit SET classname = TRANWRD(classname, '^_', '&#160;');
127  UPDATE Work.Junit SET type = put (TRANWRD(type, '^_', '&#160;'),$32.);
128  QUIT;
129 
130 
131  /* Print the aggregated table with test information using the JUnit-Tagset */
132  options linesize=256;
133  ods listing close;
134  ods tagsets.JUnit_XML file="&o_file.";
135  PROC PRINT data = Work.JUnit;
136  RUN;
137  ods _all_ close;
138  ods listing;
139 
140 %MEND _reportJUnitXML;
141