开发者

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 examp开发者_如何学Pythonle 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

  • Transformiix Test Cases

  • Running XSLT at the Department: Command Line XSLT Processing

  • Building TransforMiiX standalone - Archive of obsolete content | MDN

  • OASIS XSLT Conformance TC Public Documents

  • Using XSLT to Assist Regression Testing

  • MicroHowTo: Process an XML document using an XSLT stylesheet

  • Tip: Debug stylesheets with xsl:message

  • Batch XSLT Processing

  • Embedded Stylesheet Modules: XSL Transformations (XSLT) Version 3.0

  • Multi layer conditional wrap HTML with XSLT

  • XPath 1.0: Axes

  • CentOS 7.0 - man page for xsltproc

  • XMLStarlet command line XML toolkit download | SourceForge.net


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜