wiki:TestingXSLT/UnitTestingXSLT

Unit Testing XSLT

Jeni Tennison’s unit testing framework, available from  http://www.jenitennison.com/xslt/utilities/unit-testing/ under the title “Unit Testing XSLT”, is a pure XSLT 2.0 solution where unit tests may be either in the stylesheet being tested or in a separate file. The following example from Jeni’s web site shows a simple test of an XSLT 2.0 function:

<test:tests>
  <test:test>
    <test:param name="number" select="2" />
    <test:expect select="4" />
  </test:test>
</test:tests>
<xsl:function name="eg:square" as="xs:double">
  <xsl:param name="number" as="xs:double" />
  <xsl:sequence select="$number * $number" />
</xsl:function>

A stylesheet containing tests and templates is transformed using a provided stylesheet to generate a new, standalone stylesheet that contains only the tests and that imports the original stylesheet. Running this stylesheet (irrespective of what you use for input) runs the tests and produces an XML result file. This result is then transformed using another provided stylesheet to produce a HTML report (defaulting to using Jeni’s distinctive purple and green colour scheme) that summarises the results.

Figure 1 shows the report generated using the above test plus a second test that is forced to fail (since "2 * 2" does not equal "5") to show the details that are provided for failed tests.

Unit Testing XSLT report. Figure 1 – Unit test report