jUnit format
authorClaus Gittinger <cg@exept.de>
Fri, 05 Aug 2011 16:01:09 +0200
changeset 307 db569def6494
parent 306 fba6c068becb
child 308 fa5e3753dbca
jUnit format
TestResultReporter.st
--- a/TestResultReporter.st	Thu Aug 04 14:09:46 2011 +0200
+++ b/TestResultReporter.st	Fri Aug 05 16:01:09 2011 +0200
@@ -16,6 +16,7 @@
         buildSupport packages.
 
     Currently supported formats are:
+        #xml_jUnit      - a junit-like xml format
         #xml_pythonUnit - a python unit-like xml format
         #xml            - same, for backward compatibility
         #tap            - perl TAP unit test format; 
@@ -98,6 +99,34 @@
 "
 !
 
+format_xml_jUnit
+"
+    sample output for one of the st/x regression-tests looks like:
+
+   <?xml version='1.0' encoding='UTF-8' ?>
+   <testsuite errors='1' failures='0' hostname='hazelnut.osuosl.org' name='net.cars.engine.BougieTest' tests='2' time='0.017' timestamp='2007-11-02T23:13:50'>
+     <properties>
+       <property name='java.vendor' value='IBM Corporation' />
+       <property name='os.name' value='Linux' />
+       <property name='sun.boot.class.path' value='/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/vm.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/core.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/charsets.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/graphics.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/security.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmpkcs.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmorb.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmcfw.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmorbapi.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmjcefw.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmjgssprovider.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmjsseprovider2.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmjaaslm.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/ibmcertpathprovider.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/server.jar:/opt/ibm-jdk-bin-1.5.0.5a/jre/lib/xml.jar' />
+       <property name='sun.java2d.fontpath' value='' />
+       <property name='java.vm.specification.vendor' value='Sun Microsystems Inc.' />
+       <property name='ant.home' value='/home/jancumps/project/continuus/ant/distro' />
+      ...
+     </properties>
+     <testcase classname='net.cars.engine.BougieTest' name='sparkDry' time='0.0010' />
+     <testcase classname='net.cars.engine.BougieTest' name='sparkHumid' time='0.0050'>
+       <error message='humidity level too high' type='java.lang.RuntimeException'>java.lang.RuntimeException: humidity level too high
+          at net.cars.engine.Bougie.spark(Unknown Source)
+          at net.cars.engine.BougieTest.sparkHumid(BougieTest.java:36)
+  </error>
+     </testcase>
+     <system-out><!![CDATA[]]></system-out>
+     <system-err><!![CDATA[]]></system-err>
+  </testsuite>
+"
+!
+
 format_xml_perfPublisher
 "
     sample output looks like:
@@ -194,6 +223,7 @@
    "return a list of formats and short-info-string, as per supported format symbol"
 
     ^ #(
+        (#'xml_jUnit'           'a junit-like xml format')
         (#'xml_pythonUnit'      'a python unit-like xml format')
         (#'xml_perfPublisher'   'xml-based format for jenkins/hudson')
         (#'tap'                 'perl TAP unit test format')
@@ -311,6 +341,99 @@
     "Created: / 30-07-2011 / 10:28:06 / cg"
 ! !
 
+!TestResultReporter methodsFor:'reporting - xml-jUnit'!
+
+reportXml_jUnit
+    "jUnit-like XML unittest report format"
+
+    stream
+        nextPutLine: '<?xml version="1.0"?>';
+        nextPutAll: '<testsuite';
+        nextPutAll:(' tests="%1">' bindWith:result runCount);
+        nextPutAll:(' timestamp="%1">' bindWith:result timestamp printStringIso8601Format);
+        nextPutAll:(' errors="%1"' bindWith:result errors size);
+        nextPutAll:(' failures="%1"' bindWith:result failures size);
+        nextPutAll:(' hostname="%1"' bindWith:OperatingSystem getHostName);
+        nextPutAll:(' name="%1">' bindWith:result name);
+        nextPutLine: '>'.
+    stream
+        nextPutLine: '  <properties>';
+        nextPutLine: '    <property name="smalltalk.vendor" value="exept Software AG" />';
+        nextPutLine: '    <property name="smalltalk.compiler" value="Smalltalk/X" />';
+        nextPutLine:('    <property name="smalltalk.version" value="%1" />'bindWith:Smalltalk versionString);
+        nextPutLine:('    <property name="os.name" value="%1" />' bindWith:OperatingSystem osName);
+        nextPutLine: '    <property name="user.name" value="%1" />' bindWith:OperatingSystem getLoginName;
+        nextPutLine: '  </properties>'.
+
+    result passed   do:[:each|self reportXml_jUnitTest: each result: #success].
+    result failures do:[:each|self reportXml_jUnitTest: each result: #failure].
+    result errors   do:[:each|self reportXml_jUnitTest: each result: #error].
+
+    stream
+        nextPutLine: '</testsuite>'
+
+    "Created: / 05-08-2011 / 15:21:45 / cg"
+!
+
+reportXml_jUnitResultAndTraceback:test state:state
+    |stateString errorMessage|
+
+    stateString := (state = 'error') ifTrue:['error'] ifFalse:['failure'].
+    errorMessage := 'no message given'.
+
+    stream
+        nextPutLine:('<%1 message="%2">' bindWith:stateString with:errorMessage).
+
+    "
+        Prints a traceback to the stream.
+        This is dialect-specific, so we have to check...
+    "
+
+    "Smalltalk/X dialect detection..."
+    ((Smalltalk respondsTo: #isSmalltalkX) and:[Smalltalk isSmalltalkX])
+        ifTrue:[^self reportXml_jUnitTracebackStX: test].
+
+    stream
+        nextPutLine:('</%">' bindWith:stateString).
+
+    "Created: / 05-08-2011 / 15:40:09 / cg"
+!
+
+reportXml_jUnitTest: test result: testResult
+    | testClassName executionTime |
+
+    testClassName := self sunitNameOf: test class.
+
+    test executionTime isNil ifTrue:[
+        executionTime := '0.0'.
+    ] ifFalse:[
+        executionTime := ((test executionTime asMilliseconds / 1000) asFixedPoint:3) printString.
+    ].
+
+    stream
+        nextPutAll:'<testcase classname="'; nextPutAll:testClassName; nextPutLine:'"'; 
+        tab; nextPutAll:'name="'; nextPutAll: test selector; nextPutLine:'"';
+        tab; nextPutAll:'status="'; nextPutAll: testResult; nextPutLine:'"';
+        tab; nextPutAll:'time="'; nextPutAll: executionTime; nextPutLine:'"'.
+
+    testResult ~= #success ifTrue:[self reportXml_jUnitResultAndTraceback:test state:testResult].
+
+    stream nextPutLine:'</testcase>'.
+
+    "Created: / 05-08-2011 / 15:22:13 / cg"
+!
+
+reportXml_jUnitTracebackStX: test
+    [ test debug ]
+        on: GenericException
+        do: [:ex|
+            ex suspendedContext printAllOn: stream
+    ].
+
+    "Modified: / 07-12-2009 / 14:06:48 / Jan Vrany <jan.vrant@fit.cvut.cz>"
+    "Created: / 05-08-2011 / 15:41:44 / cg"
+! !
+
 !TestResultReporter methodsFor:'reporting - xml-perfPublisher'!
 
 reportXml_perfPublisher
@@ -546,11 +669,11 @@
 !TestResultReporter class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestResultReporter.st,v 1.17 2011-08-03 11:04:13 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestResultReporter.st,v 1.18 2011-08-05 14:01:09 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestResultReporter.st,v 1.17 2011-08-03 11:04:13 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestResultReporter.st,v 1.18 2011-08-05 14:01:09 cg Exp $'
 !
 
 version_SVN