wiki:TestingXSLT/XSLTunit

XSLTunit ¶

XSLTunit ( http://xsltunit.org/) by Eric van der Vlist of Dyomedea is the grandfather of XSLT unit testing frameworks. When asked, Eric described it as “stable, rustic and mature”. It has influenced, both positively and negatively, the development of several other testing projects.

XSLTunit tests are written in a stylesheet. The stylesheet imports both the stylesheet being tested and the XSLTunit “xsltunit.xsl” stylesheet. The unit tests are written within a template that matches the document root (so it is the first rule executed when the combined stylesheets are run).

This example from the XSLTunit web site illustrates how elements in the XSLTunit namespace do the work and an xsl:apply-template invokes the stylesheet being tested to produce a result that is to be compared against the expected result.

<xsl:template match="/">
  <xsltu:tests>
    <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>
…
  </xsltu:tests>
</xsl:template>

The result from running XSLTunit is an XML document indicating the success or failure of each test. When a test asserting equality with an expected result fails, the output also includes a diff of the expected and actual results. The following is the result from running the sample files from the XSLTunit web site:

<xsltu:tests xmlns:xsltu="http://xsltunit.org/0/">
  <xsltu:test id="test-title">
    <xsltu:assert id="full-value" outcome="passed"/>
  </xsltu:test>
  <xsltu:test id="test-title-reverted">
    <xsltu:assert id="non-empty-h1" outcome="passed"/>
  </xsltu:test>
  <xsltu:test id="XPath-expressions">
    <xsltu:assert id="h1" outcome="passed"/>
    <xsltu:assert id="value" outcome="passed"/>
  </xsltu:test>
</xsltu:tests>