Unit testing XSLT

The language I use most in my daily work is XSLT, version 2.0. When you have been there, there is no way back to version 1.0.
Surely if you use SAXON-SA, the schema-aware version of the leading XSLT 2.0 processor.

I have been thinking for years now to implement unit testing for my XSLT scripts, but I never arrived at this point.
The main reason being that:
For a good overview of all possibilities for testing XSLT, I refer to the article 'Testing XSLT' from Tony Graham.

On September 7 however, I saw the announcement of Jeni Tennison of XSpec. The difference with all the other frameworks including hers (Unit Testing XSLT) is that this one falls in the family of Behavior Driven Development.

So I decided to give it a try and to integrate the framework in Oxygen.
  1. Download the code from Google Code and unzip.
  2. Make sure that the batch or shell files include the correct path to SAXON.

    In my case

    export CLASSPATH=".:/Applications/oxygen 10/lib/saxon9sa.jar"
  3. Add in Oxygen under Tools -> External Tools a new command line.

    external tool in Oxygen

  4. write your XSpec document using the included RelaxNG schema

    RelaxNG schema use

    leading to this view in the editor

    start editing an XSpec file

  5. Refer in your XSpec file to the XSLT stylesheet you want to test

    <description xmlns="http://www.jenitennison.com/xslt/xspec" 
    xmlns:sc="http://www.sc.com"
    xmlns:dcterms="http://purl.org/dc/terms/"
    stylesheet="producten.xsl"
    xslt-version="2.0">...
  6. Write your testscenarios following the instructions on the XSpec Wiki

    <scenario label="productname">
    <context href="test.xml" select="/sc:indexfeed/sc:list[1]/sc:product[1]/sc:productname[1]"/>
    <expect label="becomes dcterms:title">
    <dcterms:title>Wii power</dcterms:title>
    </expect>
    </scenario>
  7. To test, run your external tool from Oxygen

    Run tests from Oxygen

  8. For getting your testresults in the browser

    Test results in the browser

Now the next step is to learn to write better tests. I keep you current.

Comments