This Version: V 0.2 (18 January 2002)
Latest Version: xsltunit.org/
This namespace: xsltunit.org/0/
Previous Version: none
XSLTunit 0.x is a proof of concept including this page, xsltunit.xsl (a XSLT sheet that can be imported into transformation to perform unit tests on XSLT sheets) and a set of examples.
The description of the namespace version convention I intend to use for XSLTunit has been posted for discussion on xml-dev.
Editor:
The purpose of XSLTunit is to provide a unit testing framework for XSLT transformations similar to the "*unit" environments available for other languages (i.e. Junit for Java).
Although not a general purpose programming language, XSLT is turing complete and allows to develop powerful (and complex) transformations that deserve unit testing.
The first limitation I have found is that stylesheets need to be slightly modified to test the template that is applied to the root element.
This is because the XSLT sheet running the test needs to define a template for the root document that will let it get control over the tests to run. After having taken control, this template has no way to invoke the template for root elements defined in the tested sheet that is imported and has a lower priority.
We will see that there is an easy workaround using modes, but the tested stylesheet has still to be edited.
A XSLTunit set of tests is a XSLT transformation that imports the tested stylesheet and xsltunit.xsl, for instance:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="www.w3.org/1999/XSL/Transform" xmlns:exsl="exslt.org/common" extension-element-prefixes="exsl" xmlns:xsltu="xsltunit.org/0/" exclude-result-prefixes="exsl"> <xsl:import class="library.xsl"/> <xsl:import class="xsltunit.xsl"/> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> .../... </xsl:stylesheet>
This example will be using a EXSLT extension to convert result tree fragments into nodeset and that's the reason why we have included the declaration of the namespace for EXSLT.
The XSLTunit namespace is used to identify modes and named templates belonging to XSLTunit as well as the vocabulary used by the test report.
This transformation will gain control (typically using a template on the document element) and generate the test report.
The document element of the test report will typically be xsltu:tests.
<xsl:template match="/"> <xsltu:tests> .../... </xsltu:tests> </xsl:template>
Let's assume we want to test that the formatting of the title of a book described in a document will have a specific value. We will write this test as: :
<xsltu:test id="test-title"> <xsl:call-template name="xsltu:assertEqual"> <xsl:with-param name="id" select="'full-value'"/> <xsl:with-param name="nodes1"> <xsl:apply-templates select="document('library.xml')/library/book[isbn='0836217462']/title"/> </xsl:with-param> <xsl:with-param name="nodes2"> <h1>Being a Dog Is a Full-Time Job</h1> </xsl:with-param> </xsl:call-template> </xsltu:test>
The named template "xsltu:assertEqual" will compare the two parameters nodes1 (that will contain the result of the transformation of the title) and nodes2 (containing the expected value) and generate the report for this test using the id defined in its id parameter).
The result will be for instance (if the test passes):
<xsltu:test id="test-title"> <xsltu:assert id="full-value" outcome="passed"/> </xsltu:test>
Or (if the test fails because the transformation has added a "!"):
<xsltu:test id="test-title"> <xsltu:assert id="full-value" outcome="failed"> <xsltu:message> <xsltu:diff name=""> <xsltu:diff name="h1"> <xsltu:no-match> <xsltu:node>Being a Dog Is a Full-Time Job!</xsltu:node> <xsltu:node>Being a Dog Is a Full-Time Job</xsltu:node> </xsltu:no-match> </xsltu:diff> </xsltu:diff> </xsltu:message> </xsltu:assert> </xsltu:test>
The test can also be reverted and we can test that two variables are different, for instance if we wanted to test that the result will not be an empty h1 element:
<xsltu:test id="test-title-reverted"> <xsl:call-template name="xsltu:assertNotEqual"> <xsl:with-param name="id" select="'non-empty-h1'"/> <xsl:with-param name="nodes1"> <xsl:apply-templates select="document('library.xml')/library/book[isbn='0836217462']/title"/> </xsl:with-param> <xsl:with-param name="nodes2"> <h1/> </xsl:with-param> </xsl:call-template> </xsltu:test>
For those situations that require more flexibility, XPath expressions can be tested using the xsltu:assert named pattern, for instance:
<xsltu:test id="XPath-expressions"> <xsl:variable name="source"> <title>My title</title> </xsl:variable> <xsl:variable name="result-rtf"> <xsl:apply-templates select="exsl:node-set($source)/title"/> </xsl:variable> <xsl:variable name="result" select="exsl:node-set($result-rtf)"/> <xsl:call-template name="xsltu:assert"> <xsl:with-param name="id" select="'h1'"/> <xsl:with-param name="test" select="$result/h1"/> <xsl:with-param name="message">This should be a h1!</xsl:with-param> </xsl:call-template> <xsl:call-template name="xsltu:assert"> <xsl:with-param name="id" select="'value'"/> <xsl:with-param name="test" select="$result/h1=$source"/> <xsl:with-param name="message">h1 is "<xsl:value-of select="$result/h1"/>"</xsl:with-param> </xsl:call-template> </xsltu:test>
This documentation has been written as a RDDL document and this section will be developed to include more resources related to XSLTunit.
This library is imported into XSLTunit sets of tests.
This release must be run using a EXSLT compliant XSLT processor (such as Saxon, libxslt, 4xslt, Xalan-J, ...).
This XSLT unit suite is the one that is decribed in the tutorial.
This instance document is used as an example in the tutorial.
This transformation is the one that is tested in the tutorial.
A CSS stylesheet borrowed from RDDL used to provide the "look-and-feel" of this document, suitable in general for RDDL documents.
Original version of the previous CSS stylesheet on rddl.org.
I have been pleasantly surprised after a couple of hours working on XSLTunit that this simple tool was beginning to be useful while still very simple (or simplistic).
The current version is already a powerful tool that can be used to perform XSLT unit testing (and XSLTunit has been used to test the XSLTunit library).
This being said, the simplicity of the tools is leaving room for many applications and extensions on which your feedback is welcome:
TBD
Many thanks to the many people that have given me hints, ideas or encouragements.
Non normative list (by chronological order): Robert Martin, James Grenning, Chuck Allison, Chet Hendricksen, Ron Jeffries, Leigh Dodds, Matt Sergeant.
Copyright (c) 2001,2003 Eric van der Vlist
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ``Software''), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ERIC VAN DER VLIST BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.