Edit online

XSLT Unit Test (XSpec)

XSpec is a behavior driven development (BDD) framework for XSLT, XQuery, and Schematron. XSpec consists of syntax for describing the behavior of your XSLT, XQuery, or Schematron code, and some code that enables you to test your code against those descriptions.

Creating an XSLT Unit Test

To create an XSLT Unit Test, go to File > New > XSLT Unit Test. You can also create an XSLT Unit Test from the contextual menu of an XSL file in the Project view. Oxygen XML Editor allows you to customize the XSpec document when you create it. In the customization dialog box, you can enter the path to an XSL document or to a main XSL document.

When you create an XSpec document based on an XSL document, Oxygen XML Editor uses information from the validation and transformation scenarios associated with the XSL file. From the transformation scenario Oxygen XML Editor uses extensions and properties of Saxon 12.3, improving the Ant scenario associated with the XSpec document.

Figure 1. New XSLT Unit Test Wizard

Running an XSLT Unit Test

To run a Unit Test, open the XSpec file in an editor and click Apply Transformation Scenario(s) on the main toolbar. This will run the built-in Run XSpec Test transformation scenario that is defined in the XSpec framework.

Testing a Stylesheet

An XSpec file contains one or more test scenarios. You can test a stylesheet in one of the following ways:
  • Test an entire stylesheet - Testing is performed in a certain context. You can define a context as follows:
    • Inline context, building the test based on a string.
      <x:scenario label="when processing a para element">
          <x:context>
              <para>...</para>
          </x:context>
          ...
      </x:scenario>  
    • Based on an external file, or on a part of an external file extracted with an XPath expression.
      <x:scenario label="when processing a para element">
          <x:context href="source/test.xml" select="/doc/body/p[1]" />
          ...
      </x:scenario>  
  • Test a function:

    <x:scenario label="when capitalising a string">
      <x:call function="eg:capital-case">
        <x:param select="'an example string'" />
        <x:param select="true()" />
      </x:call>
      ...
    </x:scenario>  
  • Test a template with a name:

    <x:scenario label="when creating a table">
      <x:call template="createTable">
        <x:param name="nodes">
          <value>A</value>
          <value>B</value>
        </x:param>
        <x:param name="cols" select="2" />
      </x:call>
    </x:scenario>

You can reference test files between each other, which allows you to define a suite of tests. For further details about test scenarios, go to https://github.com/xspec/xspec/wiki/Writing-Scenarios.

Adding a Catalog to an XSpec Transformation

If your XSLT needs a catalog, you can add one to the XSpec transformation by doing one of the following:
  • If you are using a project in Oxygen XML Editor, create a catalog.xml file in the project directory. This catalog will then be loaded automatically.
  • Edit the Run XSpec Test transformation scenario, go to the Parameters tab, and set the value of the catalog parameter to the location of your catalog file.