7 Auto-generating tests
Prolog is an interactive environment. Where users of non-interactive systems tend to write tests as code, Prolog developers tend to run queries interactively during development. This interactive testing is generally faster, but the disadvantage is that the tests are lost at the end of the session. The test-wizard tries to combine the advantages. It collects toplevel queries and saves them to a specified file. Later, it extracts these queries from the file and locates the predicates that are tested by the queries. It runs the query and creates a test clause from the query.
Auto-generating test cases is experimentally supported through the
library library(test_wizard)
. We briefly introduce the
functionality using examples. First step is to log the queries into a
file. This is accomplished with the commands below. Queries.pl
is the name in which to store all queries. The user can choose any
filename for this purpose. Multiple Prolog instances can share the same
name, as data is appended to this file and write is properly locked to
avoid file corruption.
:- use_module(library(test_wizard)). :- set_prolog_flag(log_query_file, 'Queries.pl').
Next, we will illustrate using the library by testing the predicates
from library library(lists)
. To generate test cases we just
make calls on the terminal. Note that all queries are recorded and the
system will select the appropriate ones when generating the test unit
for a particular module.
?- member(b, [a,b]). Yes ?- reverse([a,b], [b|A]). A = [a] ; No
Now we can generate the test-cases for the module list using make_tests/3:
?- make_tests(lists, 'Queries.pl', current_output). :- begin_tests(lists). test(member, [nondet]) :- member(b, [a, b]). test(reverse, [true(A==[a])]) :- reverse([a, b], [b|A]). :- end_tests(lists).