Most systems should have tests associated with them, and most of the time these tests should be invoked regularly during development. Measuring the frequency with with tests are invoked, and whether the tests actually pass or not, can be very useful for not only assessing the quality of the system, but also the quality of the development process.
StackyHack implements a set of test cases using JUnit, a popular Java testing tool.
JUnit is already installed, as discussed in Section 4.4.2, “Install JUnit”.
Example 4.10, “junit.build.xml” shows the junit.build.xml file, which defines the Ant targets used to invoke the JUnit tool and the JUnit sensor.
Example 4.10. junit.build.xml
<project name="stackyhack.junit" default="junit">
<description>
Provides the JUnit tool and the Hackystat JUnit sensor.
</description>
<import file="build.xml"/>
<property name="junit.dir" location="${build.dir}/junit" />
<target name="junit" depends="junit.tool, junit.report, junit.sensor" description="Runs JUnit, JunitReport, and the sensor."/>
<target name="junit.tool" depends="compile" description="Run JUnit tests.">
<mkdir dir="${junit.dir}" />
<!-- Run the tests, which are all classes whose name starts with 'Test'. -->
<junit printsummary="withOutAndErr" haltonfailure="${junit.haltonfailure}">
<classpath>
<pathelement location="${build.dir}/classes" />
<path refid="compile.classpath"/>
</classpath>
<formatter type="xml" />
<batchtest todir="${junit.dir}">
<fileset dir="${src.dir}" includes="**/Test*.java"/>
</batchtest>
</junit>
</target>
<target name="junit.report" description="Generates an HTML report for JUnit.">
<taskdef name="junitreport" classname="org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator" />
<junitreport todir="${junit.dir}">
<fileset dir="${junit.dir}" includes="TEST-*.xml"/>
<report format="frames" todir="${junit.dir}" />
</junitreport>
</target>
<target name="junit.sensor" description="Sends UnitTest data to Hackystat using the JUnit sensor.">
<!-- Define the junit sensor taskdef, failing the build if the sensor is not installed. -->
<available classname="org.hackystat.sensor.junit.JUnitSensor" property="junit.sensor.available"/>
<fail unless="junit.sensor.available" message="Error: JUnit sensor not installed."/>
<taskdef name="hacky-junit" classname="org.hackystat.sensor.junit.JUnitSensor" />
<!-- Send JUnit test data to hackystat using the JUnit sensor. -->
<hacky-junit verbose="${hackystat.verbose.mode}" sourcePath="${src.dir}">
<fileset dir="${junit.dir}" includes="TEST-*.xml"/>
</hacky-junit>
</target>
</project>
The junit.build.xml defines the <junit.tool> target to invoke JUnit and produce test data in XML format in the build/junit directory. The <junit.sensor> target uses the JUnit sensor to read in the XML files generated by JUnit and send the output to Hackystat. The <junit> target simply invokes both of these targets in sequence.
Example 4.11, “Invoking the junit.tool target” shows the output from invoking the <junit.tool> target to run the tests associated with the StackyHack system.
Example 4.11. Invoking the junit.tool target
C:\svn-csdl\StackyHack>ant -f junit.build.xml junit.tool
Buildfile: junit.build.xml
compile:
junit.tool:
[junit] Running edu.hawaii.stack.TestClearStack
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.063 sec
[junit] Running edu.hawaii.stack.TestStack
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.015 sec
BUILD SUCCESSFUL
Total time: 1 second
This target produces a number of XML files in the build/junit directory containing information about the tests that were invoked and the results of testing.
After JUnit has been invoked to generate data regarding the results of testing, the JUnit sensor can be invoked to read in the resulting XML files produced by JUnit and send the resulting data to Hackystat as UnitTest sensor data.
To accomplish this, you must first install the JUnit sensor, which can process the XML files output by JUnit. General instructions on installing sensors are provided in Chapter 2, Client-side configuration: Tool sensor installation; instructions for specific sensors are provided in Chapter 26, Sensors. After installation, your hackyInstaller JUnit configuration window should look something like the screen image in Figure 4.6, “ HackyInstaller configuration screen for JUnit sensor ”.
Example 4.12, “junit.sensor invocation” illustrates the invocation of the JUnit sensor.
Example 4.12. junit.sensor invocation
C:\svn-csdl\StackyHack>ant -f junit.build.xml junit.sensor Buildfile: junit.build.xml junit.sensor: [hacky-junit] Sensor enabled?: true [hacky-junit] Processing file: C:\svn-csdl\StackyHack\build\junit\TEST-edu.hawaii.stack.TestClearStack.xml [hacky-junit] Processing file: C:\svn-csdl\StackyHack\build\junit\TEST-edu.hawaii.stack.TestStack.xml [hacky-junit] Hackystat data on 3 JUnit tests sent to http://hackystat.ics.hawaii.edu/ (2 secs.) BUILD SUCCESSFUL Total time: 3 seconds
Once you've invoked the sensor, you can verify that this data was received at the server by using the List Sensor Data command. Figure 4.7, “ List Sensor Data with StackyHack JUnit data ” illustrates what this page might look like after receiving the StackyHack data.