XSpec ( http://code.google.com/p/xspec/), by Jeni Tennison and contributors, is a Behavior Driven Development (BDD) framework for XSLT. It is based on the Spec framework of RSpec, which is a BDD framework for Ruby.
XSpec consists of a syntax for describing the behaviour of your XSLT code, and some code that enables you to test your XSLT code against those descriptions.
Some aspects in which XSpec differs from Jeni's earlier Unit Testing XSLT framework are:
- Tests are defined as 'scenarios' where you describe what should happen and define what to expect, whereas in the other framework, tests were identified by ID only.
However, the ability to label tests and provide strings associated with assertions is not unique to XSpec.
- Scenarios may be nested, so multiple scenarios may inherit a common context
- Scenarios documents may import other scenario documents, promoting reuse
- Scenarios may be marked 'pending' (and a description may be provided): pending scenarios are not run, but are reported as pending in the test report, so they remain visible. This compares favourably to other frameworks where you would have to comment out any tests that can't be run at that time.
For this stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="/"> <m>Hello world!</m> </xsl:template> <xsl:template match="unused"> <m>You can't see me!</m> </xsl:template> </xsl:stylesheet>
This description document describes a single scenario:
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" stylesheet="test.xsl"> <x:scenario label="Processing document root"> <x:context select="/"><anything/></x:context> <x:expect label="Should be 'Hello world'."><m>Hello world!</m></x:expect> </x:scenario> </x:description>
Figure 1 shows the report from running XSpec on the description document.
XSpec also includes a coverage utility which highlights parts of the stylesheet under test that have not been exercised by the scenarios.
The coverage utility comprises a custom TraceListener for use with Saxon and a stylesheet for generating a report of the results.
The XSLTCoverageTraceListener produces an XML file recording the line number and module of each XSLT element in the stylesheet under test, producing an element each time an XSLT element is executed.
The stylesheet parses the stylesheet under test, and for each element, determines whether or not the element is listed in the XSLTCoverageTraceListener output. Those that are not are highlighted in the generated report.
Figure 2 shows the coverage report for the example stylesheet and description document used in the previous example. The unused template shows as white text on a red background.