XML technologies / XSLT / XSLT Cookbook / Testing and debugging / Structuring unit tests
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to organize tests to simplify testing.
Solution
Notice how recipe Adding embedded unit tests to utility stylesheets embeds test data in the test stylesheets. Each element test contains an attribute num and the expected result in attribute ans. The test harness extracts these elements test, runs the tests and compares expected and actual results. Crucially, it produces no output for successful tests.
Discussion
Some of the best advice on automated testing is in The Practice of Programming by Brian W. Kernighan and Rob Pike (Addison-Wesley, 1999). They say test programs should produce output only when tests fail. Why? Would you want to sift through pages of output looking for failures? If test code is expected to be silent, errors are easy to spot whenever it does produce output. First, of course, test the test code to ensure it really runs.
Storing the result as an attribute on element test works for simple tests producing primitive results. Some templates produce node sets; expected results may then need to be stored as child elements and compared using set operations from recipe Performing set operations on node sets using value semantics. Sometimes a simpler test is possible. Consider the test harness for the template math:lowest. Recall that math:lowest returns all nodes containing the smallest number in an input node set:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.exslt.org/math" exclude-result-prefixes="math test" xmlns:test="http://www.ora.com/XSLTCookbook/test" id="math:math.lowest">
<xsl:import href="math.min.xslt"/>
<xsl:template name="math:lowest">
<xsl:param name="nodes" select="/.."/>
<xsl:variable name="min">
<xsl:call-template name="math:min">
<xsl:with-param name="nodes" select="$nodes"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="number($min) = $min">
<xsl:copy-of select="$nodes[. = $min]"/>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
<!-- TEST-CODE: NICHT ENTERNEN! -->
<xsl:template match="xsl:stylesheet[@id='math:math.lowest'] | xsl:include[@href='math.lowest.xslt'] " xmlns:exsl="http://exslt.org/common">
<xsl:message>TESTING math.lowest</xsl:message>
<xsl:choose>
<xsl:when test="function-available('exsl:node-set')">
<xsl:for-each select="document('')/*/test:test">
<xsl:variable name="ans">
<xsl:call-template name="math:lowest">
<xsl:with-param name="nodes" select="test:data"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="$ans-ns" select=" exsl:node-set($ans)"/>
<xsl:if test="not($ans-ns/* != test:data[. = current( )/@ans]) and count($ans-ns/*) != count(test:data[. = current( )/@ans])">
<xsl:message>math:lowest TEST von <xsl:value-of select="@num"/> FEHLGESCHLAGEN [<xsl:copy-of select="$ans-ns"/>] [<xsl:copy-of select="test:data[. = current( )/@ans]"/>]</xsl:message>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:message>WARNUNG: Testcode von math.lowest benötigt exsl:node-set THIS VERSION=[<xsl:value-of select="system-property('xsl:version')"/>] VENDOR=[<xsl:value-of select="system-property('xsl:vendor')"/>]</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<test:test num="1" ans="1" xmlns="http://www.ora.com/XSLTCookbook/test">
<data>9</data>
<data>8</data>
<data>7</data>
<data>6</data>
<data>5</data>
<data>4</data>
<data>3</data>
<data>2</data>
<data>1</data>
</test:test>
<!-- weitere Tests hier ... >
</xsl:stylesheet>The comparison relies on the behavior of != when both sides are node sets. It returns true if a pair, one node from each set, has differing string values. Verify that nodes selected as expected results from the test set are identical to those returned by math:lowest and that their counts agree.
Some calculations, especially mathematical approximations, are correct even if they do not exactly match the theoretical result. Specify a tolerance in the test and check that the calculated and correct results agree within it.
See also
Brian W. Kernighan and Rob Pike’s The Practice of Programming (Addison-Wesley, 1999) is not specific to XSLT but gives excellent testing and debugging advice for all kinds of programs.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de