Transcript-style tests are text files containing R commands and output, as
though copied verbatim from an interactive R session. The output in the
transcript file must match the actual output from running the command
for the test to pass (with some exceptions - see "Control over matching"
below). This testing framework is identical in concept to the standard
.R/.Rout.save
tests that are run by the R CMD check
, but
with some enhancements that are intended to make test development and
maintanance faster, more convenient, and easier to automate:
.Rt
file, which is analogous to an
.Rout.save
file)
R CMD check
stopping with an
indication of error after running all tests). Unlike the standard
tests in R CMD check
, output mismatch detected by
scriptests
results in R CMD check
stopping with an error.
To make it so that "R CMD check" will run transcript-style tests, do the following:
scriptests
package is installed on your system
library(scriptests) runScripTests()
> 1 + 2 [1] 3 >
Suggests: scriptests
to DESCRIPTION file.
If there is an existing "Suggests:" line, just add scriptests
to it. (It's better to use the Suggests:
than the
Depends:
fields, because packages listed in the depends field
are loaded when the package is loaded for normal use, and the
scriptests
package is usually not needed for normal use of a package --
the scriptests
package will only be needed for testing.
At the end of testing, the file test-summary.txt
will be left in
the tests
directory. To be entirely sure that the tests were
run, also check for the existence of test-summary.txt
.
If any tests fail, the file test-summary.fail
(a copy of
test-summary.txt
) will also be left in the tests
directory
-- the existence of this file can be used in a programmatic check for
whether all tests passed.
Tests can be run interactively using the function
runtests()
. The function source.pkg()
can
be useful to quickly re-read the function definitions in a package
during code development. See the section "Running tests" in the
documentation for runtests()
for further details.
Notes:
library(package-being-tested)
command before the tests. If
this heuristic fails, the functions from the package being tested
may not be accessible. If this problem occurs, it can be worked
around by explicitly including a
library(package-being-tested)
command at the beginning of
each .Rt
file.
options(error=function() NULL)
at the beginning
of the transcript file. This will cause the non-interactive R
session that runs the commands in the scripts to continue after an
error, instead of stopping, which is the default behavior for
non-interactive R.
Control over matching
Actual output is matched to desired output extracted from the
transcript file in a line-by-line fashion. If text is wrapped
differently over multiple lines, the tests will fail (unless
ignore-linebreaks
is used). Different output width can easily
happen if options("width")
was different in the session that
generated the desired output. Before trying to match, scriptests
converts all white-space to single white-space, unless a control
line specifies otherwise.
The following control lines can be present in the transcript after a command and before its output:
> cat("The date is <", date(),="" "="">\n", sep="") #@gsub("<[^>]*>", "", both) The date is> [^>",>
The tests
directory can also contain a CONFIG
file, which can
specify the functions to call for testing. The defaults are equivalent
to the following lines in the CONFIG
file:
Depends: scriptests Debug: FALSE Rsuffix: R StopOnError: FALSE Initialize: scriptests:::initializeTests() Diff: scriptests:::ScripDiff() Finalize: scriptests:::summarizeTests()The
CONFIG
file is optional and is not needed in ordinary usage.