Database Reference
In-Depth Information
ment (BDD), which provide guidance on how to deliver robust and well-tested soft‐
ware.
Many programming languages offer various integrated or third-party tools and libra‐
ries for facilitating the structured testing of applications. While XQuery is a very
high-level functional language, it should certainly not be considered exempt from
good testing practices. Thus far, there have been many attempts at creating frame‐
works for testing XQuery modules and functions, including XTC, XQUT, XSpec,
XRay, and Unit Module. Most of these frameworks are, unfortunately,
implementation-specific, due to the current lack of reflective capabilities in XQuery.
There are many different types of tests that can be constructed, but all of the XQuery
test frameworks to date have focused on the unit testing type of tests. Unit tests are
designed to allow you to assert the behavior of a small unit of code, ideally in isola‐
tion from the rest of the system. In this chapter, we will also focus on writing unit
tests in XQuery.
For many years eXist has provided an XQuery testing mechanism within its own test
suite to its core developers, allowing them to write tests in XQuery to assert the cor‐
rect behavior of eXist. Called XQuery Test Runner, it was never particularly designed
with the needs of other XQuery implementations or developers in mind, and it
proved somewhat clunky as test suite descriptions had to be written in a separate file
using a specific XML DSL.
As of version 2.1 eXist now officially provides XQSuite , a unit test framework
designed to be used by any XQuery developer and in such a way that it could be
implemented by any XQuery 3.0 vendor. XQSuite provides a standard set of XQuery
3.0 function annotations that set up test parameters and make assertions about the
results. One of the most interesting characteristics of XQSuite is that it allows you to
place your tests directly onto the function you wish to test. Should you wish, how‐
ever, you can also choose to keep your tests separate from your code (in a different
module), by creating wrapper functions that have the XQSuite annotations that sim‐
ply call your functions under test. Even if you do choose to place the XQSuite annota‐
tions onto the functions under test, your code is still potentially portable to platforms
other than eXist, as the XQuery 3.0 specification states that if an implementation
does not understand an annotation, it can just ignore it.
Perhaps the best way to learn how to use XQSuite is to look at some examples. The
examples used here are supplied in the chapters/advanced-topics/xquery-testing folder
of the book-code Git repository (see “Getting the Source Code” on page 15 ). Imagine
that you have an XQuery library module for producing identifiers, as shown in
Example 16-1 (see the file id.xqm ).
Search WWH ::




Custom Search