Words in Boxes

Nouns, verbs, and occasionally adjectives.

Thursday, December 04, 2008

Getting Started With XSpec

Lately I've been working with XSpec, a new testing framework for XSLT created by Jeni Tennison. I dabbled with Jeni's old testing framework, Tennison Tests, but I never could muster the discipline to use it as much as I should. XSpec makes testing frictionless, which is really how it must be if I'm going to make it a habit. 

Here's how to integrate it into Oxygen:

xspec_toolbar

On Windows, XSpec is launched by running a batch file. It's simple to hook up this batch file to Oxygen as an external tool so that you can run an XSpec suite by clicking a button.  By being a little clever - and sticking to a consistent naming convention - we can set it up so that we can activate the tests either from the XSpec document itself, or the XSLT you're developing.  The caveat is that you must save your XSpec documents in the same folder as the XSLT, and give it the same name as that transform, except with the extension "xspec" instead of "xslt." (For example, my_stylesheet.xspec tests my_stylesheet.xslt.) If you want to follow a different convention, adjust the following instructions appropriately.

  1. Download XSpec and unzip it somewhere in your system path. I recommend getting the latest xspec.bat and transforms, since Jeni's fixed a few bugs from the initial release.
  2. Open up xspec.bat, and edit the third line to set the CP (or class path) variable to the location where Saxon is installed. (If you don't have Saxon, get it).
  3. Now we need to add XSpec as an external tool in Oxygen. To do so, go to Tools > External Tools > Preferences > New and fill out the fields like so:

    XSpec_oxygen

    (Make sure to change the paths to match those on your computer.)

  4. I like to tell Oxygen to make XSpec-namespaced elements a different color. To do that, go to Options > Preferences > Editor > Colors > Elements by Prefix.  This makes it easier to differentiate between XSpec instructions, and the inline XML test data.

Now you should have a button on your toolbar labeled "Run XSpec Test." If it doesn't show up, make sure to activate the "External Tools" toolbar.

Here's an example of a successful test report:

xspec_report

I'm James Sulak, a software developer in Houston, Texas.

You can also find me on Twitter, or if you're curious, on my old-fashioned home page. If you want to contact me directly, you can e-mail comments@wordsinboxes.com.