Automating the extraction of test assertions from a specification

The Mobile Web Test Suites Working Group worked with the editors of Widgets family of specifications in the Web Applications Working Group on a methodology that has allowed to automate the extraction of test assertions from their specifications, and their maintenance in parallel to the building of the test suite.

Making the specification testable

Most W3C specifications use the RFC 2119 keywords (MUST, SHOULD, MAY, etc.) to indicate the level of requirement that is imposed on an implementation of the specification.

Taking an example from the Widgets Packaging and Configuration specification:

If a user agent encounters a file matching a file name given in the file name column of the default start files table in an arbitrary folder, then the user agent MUST treat that file as an arbitrary file.

After following the definitions given in the specification, the conformance requirement above can be easily turned into a testable assertion (which itself can be used to derive test cases):

Upon encountering a file matching either index.htm, index.html, index.svg, index.xhtml, or index.xht in a folder different from the root directory and the locales directory, a user agent does not treat this file as a default start file, a default icon, or a configuration file.

Given their similarities, a conformance requirement can be used as an equivalent to a testable assertion, provided it contains all the necessary information to make it testable (see The Structure of a Test Assertion in the Test Assertions Guidelines, OASIS), namely:

There are common mistakes in writing conformance requirements that makes them much harder to use as testable assertions, including:

The first step in turning the specification into a useful source of testable assertions is logically to fix as many of these problems as possible. In the process of doing so, it is possible to use specific mark-up conventions that can facilitate the creation and maintenance of the test suite.

Mark-up conventions for test assertions

To make it easy to create test cases based on a given test assertion, that test assertion minimally needs:

Using mark-up conventions make it possible to automatically extract the test assertions from the specifications with the required information; re-using the example of test assertion above, the following mark-up highlights the important information:

<p id="ta-RRZxvvTFHx">If a user agent encounters a <a href="#file">file</a> matching a file name given in the file name column of the <a href="#default-start-files-table">default start files table</a> in an <a href="#arbitrary">arbitrary</a> <a href="#folder">folder</a>, then <a class="product-ua" href="#user-agent">user agent</a> <em class="ct">MUST</em> treat that file as an <a href="#arbitrary">arbitrary</a> file.</p>

That mark-up achieves the following results:

Extracting automatically test assertions

There are a number of technologies that can be used to filter an HTML document based on the kind of mark-up conventions described above.

In the course of the co-development of the test suite and the specification, the group and the specification editors used two of these technologies:

The original extraction of test assertions was made through an XSLT style sheet, that allowed to generate a static list of test assertions that served as the first basis for the review of the testability of the specification.

Over time, the extraction system was switched to the JavaScript-based approach, allowing for an easier maintenance of the test plan where the test assertions could be automatically obtained from the specification, while keeping the rest of the test plan easy to update.

Test assertions and test cases

Once the test assertions have been identified and extracted, the work of creating test cases for each test assertion is vastly simplified.

A test writer can look at a given test assertion, create a test case that matches the pre-requisites set in the assertion, and document the expected outcome described by the required behaviour.

Each test case can be associated to a given test assertion ; later on, when running the test suite and finding test cases that fail, it allows to identify very quickly the assertion behind it that was failed, and thus evaluate which of the implementation, the test case or the specification is wrong.

To maintain the association between test cases and test assertions, a simple XML file was set up:

<testsuite for="http://www.w3.org/TR/widgets/">
  <test id="aa"
    for="ta-ACCJfDGwDQ"
    src="test-cases/ta-ACCJfDGwDQ/000/aa.wgt"> Tests that
    the UA rejects configuration documents that don't have correct
    widget element at the root. To pass, the UA must treat this as an
    invalid widget (the root element is not widget).></test>
</testsuite>

The test element encompasses data about a single test case:

This XML file allows to generate the final round of packaging and information needed for the test suite:

While the methodology described above uses three separate steps (making the specification testable, marking it up and linking test assertions to test cases), these steps don't have to be applied sequentially, and have shown in practice to work best in a more integrated and iterative process.

This methodology has proved very successful for the Widgets Packaging and Configuration specification, and is now being applied to the other Widgets specifications developed by the Web Applications Working Group.