Unit Module

From BaseX Documentation
Revision as of 18:24, 23 May 2014 by CG (talk | contribs) (→‎Example)
Jump to navigation Jump to search

This XQuery Module contains annotations and functions for performing Unit tests with XQuery.

Introduction

The more complex a software application grows, the more error-prone it gets. This is why testing frameworks have been developed, which provide a standardized, automatized way for testing software. The XUnit frameworks (such as SUnit or JUnit) allow testing of atomic unit of a program, such as single functions and algorithms.

This module borrows heavily from the existing frameworks: it introduces various new annotations for testing XQuery functions. Unit functions are provided to assert the validity of arbitrary conditions expressed in XQuery and to raise errors whenever a condition is not satisfied. Some additional functions exist to run all unit tests of the current module or a set of specified library modules.

Please note that this module is still in beta stage, and its functionality is still subject to change. Your feedback is welcome.

Conventions

Both functions and errors in this module are assigned to the http://basex.org/modules/unit namespace, which is statically bound to the unit prefix.

Annotations

%unit:test

Syntax %unit:test
%unit:test("expected", <ERROR>)
Summary With this annotation, a function can be marked as unit test. It will be evaluated whenever a test report is created for the module in which this function is located.
If an optional error code is specified and if the function expression does not raise that error, the test will fail.

%unit:before

Syntax %unit:before
Summary A function decorated with this annotation will be evaluated before each unit test.

%unit:after

Syntax %unit:after
Summary A function decorated with this annotation will be evaluated after each unit test.

%unit:before-module

Syntax %unit:before-module
Summary If a function is decorated with this annotation, it will be evaluated before all unit tests in the current module.

%unit:after-module

Syntax %unit:after-module
Summary If a function is decorated with this annotation, it will be evaluated after all unit tests in the current module.

%unit:ignore

Syntax %unit:ignore
%unit:ignore("message")
Summary If a function is decorated with this annotation, it will temporarily be ignored by the test suite runner.

Functions

unit:assert

Signatures unit:assert($test as item()*) as empty-sequence()
unit:assert($test as item()*, $message as xs:string) as empty-sequence()
Summary Asserts that the effective boolean value of the specified $test is true and returns an empty sequence. Otherwise, raises an error. The effective boolean value of an expression can be explicitly computed by using the fn:boolean function.
The unit failure message overridden with the $message string.
Errors UNIT0001: the assertion failed, or an error was raised.

unit:assert-equals

Template:Mark

Signatures unit:assert-equals($returned as item()*, $expected as item()*) as empty-sequence()
unit:assert-equals($returned as item()*, $expected as item()*, $message as xs:string) as empty-sequence()
Summary Asserts that the specified arguments are equal according to the rules of the fn:deep-equals function. Otherwise, raises an error.
The unit failure message overridden with the $message string.
Errors UNIT0001: the assertion failed, or an error was raised.

unit:fail

Signatures unit:fail($message as xs:string) as empty-sequence()
Summary Raises a unit error with the specified message.
Errors UNIT0001: default error raised by this function.

unit:test

Template:Mark enhanced test report output

Signatures unit:test() as element(testsuite)*
unit:test($functions as function(*)*) as element(testsuite)*
Summary Runs all functions, or the specified $functions, in the current query context that have unit annotations.
A test report is generated and returned, which resembles the format returned by other xUnit testing frameworks, such as the Maven Surefire Plugin.
Errors UNIT0002: a test function must have no arguments.
UNIT0003: a test function must not be updating.
UNIT0004: an annotation was declared twice.
UNIT0005: an annotation has invalid arguments.
Examples unit:test(inspect:context()) runs all unit tests found in the query context.

unit:test-uris

Template:Mark enhanced test report output

Signatures unit:test-uris($uris as xs:string*) as element(testsuites)
Summary Runs all functions in the specified modules that have unit annotations.
A test report is generated and returned, which resembles the format returned by other xUnit testing frameworks, such as the Maven Surefire Plugin.
Errors UNIT0002: a test function must have no arguments.
UNIT0003: a test function must not be updating.
UNIT0004: an annotation was declared twice.
UNIT0005: an annotation has invalid arguments.

Example

The following XQuery main module creates a test report. It contains all available unit annotations:

Query:

(:~ Initializing function, which is called once before all tests. :)
declare %unit:before-module function unit:before-all-tests() {
  ()
};
 
(:~ Initializing function, which is called once after all tests. :)
declare %unit:after-module function unit:after-all-tests() {
  ()
};
 
(:~ Initializing function, which is called before each test. :)
declare %unit:before function unit:before() {
  ()
};
 
(:~ Initializing function, which is called after each test. :)
declare %unit:after function unit:after() {
  ()
};
 
(:~ Function demonstrating a successful test. :)
declare %unit:test function unit:assert-success() {
  unit:assert(<a/>)
};
 
(:~ Function demonstrating a failure using unit:assert. :)
declare %unit:test function unit:assert-failure() {
  unit:assert((), 'Empty sequence.')
};
 
(:~ Function demonstrating a failure using unit:assert-equals. :)
declare %unit:test function unit:assert-equals-failure() {
  unit:assert-equals(4 + 5, 6)
};
 
(:~ Function demonstrating an unexpected success. :)
declare %unit:test("expected", "FORG0001") function unit:unexpected-success() {
  ()
};
 
(:~ Function demonstrating an expected failure. :)
declare %unit:test("expected", "FORG0001") function unit:expected-failure() {
  1 + <a/>
};
 
(:~ Function demonstrating the creation of a failure. :)
declare %unit:test function unit:failure() {
  unit:fail("Failure!")
};
 
(:~ Function demonstrating an error. :)
declare %unit:test function unit:error() {
  1 + <a/>
};
 
(:~ Skipping a test. :)
declare %unit:test %unit:ignore("Skipped!") function unit:skipped() {
  ()
};

Result:

<testsuite name="file:///C:/Users/user/Desktop/x.xq" time="PT0S" tests="8" failures="4" errors="1" skipped="1">
  <testcase name="assert-success" time="PT0S"/>
  <testcase name="assert-failure" time="PT0S">
    <failure line="28" column="15" type="UNIT0001">Empty sequence.</failure>
  </testcase>
  <testcase name="assert-equals-failure" time="PT0S">
    <failure line="33" column="22">
      <returned item="1">9</returned>
      <expected item="1">6</expected>
    </failure>
  </testcase>
  <testcase name="unexpected-success" time="PT0S">
    <failure>
      <expected>FORG0001</expected>
    </failure>
  </testcase>
  <testcase name="expected-failure" time="PT0S"/>
  <testcase name="failure" time="PT0S">
    <failure line="48" column="13" type="UNIT0001">Failure!</failure>
  </testcase>
  <testcase name="error" time="PT0S">
    <error line="53" column="6" type="FORG0001">Invalid xs:double cast: "".</error>
  </testcase>
  <testcase name="skipped" skipped="Skipped!" time="PT0S"/>
</testsuite>

Errors

Code Description
UNIT0001 An assertion failed, or an error was raised.
UNIT0002 A test function must have no arguments.
UNIT0003 A test function must not be updating.
UNIT0004 An annotation was declared twice.
UNIT0005 An annotation has invalid arguments.

Changelog

Changelog

Version 7.8

This module was introduced with Version 7.7.