Edit online

XQuery 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 XQuery Unit Test

To create a XQuery Unit Test, go to File > New > XQuery Unit Test. This is simple document template to help you get started.

Running an XQuery 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 an XQuery file

An XSpec file contains one or more test scenarios.

Example:

Suppose you have this XQuery function that takes a string as its argument. The first character of the string passed to the function is capitalized and concatenated to the rest of the string. Finally, the new string with the first character capitalized is returned to the calling method.
module namespace functx = "http://www.functx.com";

declare function functx:capitalize-first
($arg as xs:string?) as xs:string? {

concat(upper-case(substring($arg, 1, 1)),
substring($arg, 2))
};
The XSpec test invokes the XQuery function and passes the string hello as a parameter. The expected value that should be returned by the function (the string Hello) is contained in the @select attribute of the x:expect element.
<x:scenario label="Calling function capitalize-first">
  <x:call function="functx:capitalize-first">
    <x:param select="'hello'"/>
  </x:call>
  
  <x:expect label="should capitalize the first character of the string" select="'Hello'"/>
  
</x:scenario>

For more details about how to write XQuery tests and various samples, see https://github.com/xspec/xspec/wiki/Getting-Started-with-XSpec-and-XQuery.