简体   繁体   中英

XSLT Unit testing

Does anyone know of a way to write unit tests for the XSLT transformation?

I've a lot of XSLT files and it's getting harder to test them manually. We have an example XML and can compare it to the resulting output XML from the XSL transormation. However, I'm looking for a better test method.

I am currently looking for some good options to do this as well. As a result, I came across this question, and a few other potential candidate solutions. Admittedly, I haven't tried any of them yet, so I can't speak to their quality, but at least they are some other avenues potentially worthy of researching.

  1. Jenni Tennison's Unit Testing Package
  2. UTF-X Unit Testing Framework
  3. Juxy
  4. XTC

Additionally, I found the following article to be informative in terms of a general methodology for unit testing XSLT.

Unit test XSL transformations

Try XSpec , a testing framework for XSLT. It allows you to write tests declaratively, and test templates and functions.

Looks like Oxygen editor has Unit Testing available as well. It " provides XSLT Unit Test support based on XSpec ".

I haven't tried it myself, but will soon.

Here are a few simple solutions:

  • Use xsltproc with a mock XML file:

     xsltproc test.xsl mock.xml
  • XSLT Cookbook - Chapter 13

  • Create a document() placeholder variable and comment/uncomment it manually:

     <xsl:variable name="Data" select="descendant-or-self::node()"/> <!-- <xsl:variable name="Data" select="document('foo.xml')" /> --> <xsl:if test="$Data/pagename='foo'"> <p>hi</p> </xsl:if>
  • Create a condition to swap the comment programmatically:

     <xsl:variable name="Data"> <xsl:choose> <!-- If source XML is inline --> <xsl:when test="descendant-or-self::node()/pageName='foo'"/> <xsl:value-of select="descendant-or-self::node()"/> </xsl:when> <!-- If source XML is external --> <xsl:otherwise> <xsl:value-of select="document('foo.xml')" /> </xsl:otherwise> </xsl:choose> </xsl:variable>

Use a shell script to inline the data programmatically in the build to automate the tests completely.

References

We have been using Java based unit test cases, in which we provide expected xml string after transformation and input xml string which needs to be transformed using some XSL. Refer to following package if you want to explore more.

org.custommonkey.xmlunit.Transform
org.custommonkey.xmlunit.Diff
org.custommonkey.xmlunit.DetailedDiff

I´m using this tool: jxsltunit .

The test is defined by an XML file which is then passed to the tool. This is an example of the test configuration:

<xsltTestsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="jxsltunit jxslttestsuite.xsd" xmlns="jxsltunit"
    description="Testsuite Test"
    xml="min-test.xml"
    xslt="min-test.xslt"
    path="pa > ch">
    <xsltTestcase match_number="0">
        <![CDATA[<ch>child 1</ch>]]>
    </xsltTestcase>
    <xsltTestcase match_number="1">
        <![CDATA[<ch>child 2</ch>]]>
    </xsltTestcase>
</xsltTestsuite>

It takes the XML, the XSL and a path in the transformed XML which gets tested. The path can contain a list which elements are identified by their index.

One benefit of this tool is that it can output the results as a junit XML file. This file can be picked up by your Jenkins to show the XLST-tests in your test results. Just add the call to the tool as a build step.

Try Jenni Tennison's Unit Testing Package (XSpec), which is a unit test and behaviour-driven development (BDD) framework for XSLT, XQuery, and Schematron. It is based on the Spec framework of RSpec, which is a BDD framework for Ruby.

With XSpec you can test XLT template wise or XPath wise per your need. For an overview on how to use/handle/write (installation|execution) click https://github.com/xspec/xspec/wiki/What-is-XSpec

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM