knitr::opts_chunk$set(
  collapse = TRUE,
  warning = TRUE,
  message = TRUE,
  width = 120,
  comment = "#>",
  fig.retina = 2,
  fig.path = "README-"
)

The first vignette demonstrates the process of applying autotest at all stages of package development. This vignette provides additional information for those applying autotest to already developed packages, in particular through describing how tests can be selectively applied to a package. By default the autotest_package() function tests an entire package, but testing can also be restricted to specified functions only. This vignette will demonstrate application to a few functions from the stats package, starting by loading the package.

library (autotest)
devtools::load_all (".", export_all = FALSE)

1. .Rd files, example code, and the autotest workflow

To understand what autotest does, it is first necessary to understand a bit about the structure of documentation files for R package, which are contained in files called ".Rd" files. Tests are constructed by parsing individual .Rd documentation files to extract the example code, identifying parameters passed to the specified functions, and mutating those parameters.

The general procedure can be illustrated by examining a specific function, for which we now choose the cor function, because of its relative simplicity. The following lines extract the documentation for the cor function, a .html version of which can be seen by clicking on the link above. Note that that web page reveals the name of the .Rd file to be "cor" (in the upper left corner), meaning that the name of the .Rd file is "cor.Rd". The following lines extract that content, first by loading the entire .Rd database for the stats package.

rd <- tools::Rd_db (package = "stats")
cor_rd <- rd [[grep ("^cor\\.Rd", names (rd))]]

The database itself is a list, with each entry holding the contents of one .Rd file in an object of class r class(cor_rd), which is essentially a large nested list of components corresponding to the various .Rd tags such as \arguments, \details, and \value. An internal function from the tools package can be used to extract individual components (using the ::: notation to access internal functions). For example, a single .Rd file often describes the functionality of several functions, each of which is identified by specifying the function name as an "alias". The aliases for the "cor.Rd" file are:

tools:::.Rd_get_metadata (cor_rd, "alias")

This one file thus contains documentation for those four functions. Example code can be extracted with a dedicated function from the tools package:

tools::Rd2ex (cor_rd)
tools::Rd2ex (cor_rd)

This is the entire content of the \examples portion of "cor.Rd", as can be confirmed by comparing with the online version.

2. Internal structure of the autotest workflow

For each .Rd file in a package, autotest tests the code given in the example section according to the following general steps:

  1. Extract example lines from the .Rd file, as demonstrated above;
  2. Identify all function aliases described by that file;
  3. Identify all points at which those functions are called;
  4. Identify all objects passed to those values, including values, classes, attributes, and other properties.
  5. Identify any other parameters not explicitly passed in example code, but defined via default value;
  6. Mutate the values of all parameters according to the kinds of test described in autotest_types().

Calling autotest_package(..., test = FALSE) implements the first 5 of those 6 steps, and returns data on all possible mutations of each parameter, while setting test = TRUE actually passes the mutated parameters to the specified functions, and returns reports on any unexpected behaviour.

3. autotest-ing the stats::cov function

The preceding sections describe how autotest actually works, while the present section demonstrates how the package is typically used in practice. As demonstrated in the README, information on all tests implemented within the package can be obtained by calling the autotest_types() function. The main function for testing package is autotest_package(). The nominated package can be either an installed package, or the full path to a local directory containing a package's source code. By default all .Rd files of a package are tested, with restriction to specified functions possible either by nominating functions to exclude from testing (via the exclude parameter), or functions to include (via the functions parameter). The functions parameter is intended to enable testing only of specified functions, while the exclude parameter is intended to enable testing of all functions except those specified with this parameter. Specifying values for both of these parameters is not generally recommended.

3.1 Listing tests without conducting them

The following demonstrates the results of autotest-ing the cor function of the stats package, noting that the default call uses test = FALSE, and so returns details of all tests without actually implementing them (and for this reason we name the object xf for "false"):

xf <- autotest_package (package = "stats",
                        functions = "cor")
xf <- autotest_package (package = "stats",
                        functions = "cor")

That reveals that the example code from the .Rd file has four main points at which the aliases defined in that file are tested: once each for var and cor, and twice for cov. The result looks like this:

print (xf)
print (xf)

The object returned from autotest_package() is a simple tibble, with each row detailing one test which would be applied to the each of the listed functions and parameters. Because no tests were conducted, all tests will generally have a type of "dummy". In this case, however, we see the following:

table (xf$type)

In addition to the r length (which (xf$type == "dummy")) dummy tests, the function also returns r length (which (xf$type == "warning")) warnings, the corresponding rows of which are:

xf [xf$type != "dummy", c ("fn_name", "parameter", "operation", "content")]

Although the auotest package is primarily intended to apply mutation tests to all parameters of all functions of a package, doing so requires identifying parameter types and classes through parsing example code. Any parameters of a function which are neither demonstrated within example code, nor given default values can not be tested, because it is not possible to determine their expected types. The above result reveals that neither the use parameter of the var function, nor the y parameter of cov, are demonstrated in example code, triggering a warning that these parameter are unable to be tested.

3.2 Conducting tests

The r length (which (xf$type == "dummy")) tests listed above with type == "dummy" can then be applied to all nominated functions and parameters by calling the same function with test = TRUE. Doing so yields the following results (as an object names xt for "true"):

xt <- autotest_package (package = "stats",
                        functions = "cor",
                        test = TRUE)
xt <- autotest_package (package = "stats",
                        functions = "cor",
                        test = TRUE)
print (xt)
print (xt)

And the r length (which (xf$type == "dummy")) tests yielded r nrow (xt) unexpected responses. The best way to understand these results is to examine the object in detail, typically through edit(xt), or equivalently in RStudio, clicking on the listed object. The different types of tests which produced unexpected responses were:

table (xt$operation)

Two of those reflect the previous results regarding parameters unable to be tested, while the remainder come from only two types of tests. Information on the precise results is contained in the content column, although in this case it is fairly straightforward to see that the operation "upper case character parameter" arises because the use and method parameters of the cor and cov functions are case-dependent, and are only accepted in lower case form. The other operation is the conversion of vectors to list-column format, as described in the first vignette.

3.4 Controlling which tests are conducted

The test parameter of the autotest_package() function can be used to control whether all tests are conducted or not. Finer-level control over tests can be achieved by specifying the test_data parameter. This parameter must be an object of class autotest_package, as returned by either the autotest_types()] or [autotest_package()](https://ropenscilabs.github.io/autotest/reference/autotest_package.html) functions. The former of these is the function which specifies all unique tests, and so returns a relatively smalltibbleofr nrow(autotest_types())` rows. The following lines demonstrate how to switch off the list-column test for all functions and parameters:

types <- autotest_types()
types$test [grep ("list_col", types$test_name)] <- FALSE
xt2 <- autotest_package (package = "stats",
                         functions = "cor",
                         test = TRUE,
                         test_data = types)
types <- autotest_types()
types$test [grep ("list_col", types$test_name)] <- FALSE
xt2 <- autotest_package (package = "stats",
                         functions = "cor",
                         test = TRUE,
                         test_data = types)
print (xt2)
print (xt2)

The result now has four rows with test == FALSE, and type == "no_test", indicating that these tests were not actually conducted. That also makes apparent the role of these test flags. When initially calling autotest_package() with default test = FALSE, the result contains a test column in which all values are TRUE. Although potentially perplexing at first, this value must be understood in relation to the type column. A type of "dummy" indicates that a test has not been conducted, in which case test = TRUE is a control flag used to determine what would be conducted if these data were submitted as the test_data parameter. For all type values other than "dummy", the test column specifies whether or not each test was actually conducted.

The preceding example showed how the results of autotest_types() can be used to control which tests are implemented for an entire package. Finer-scale control can be achieved by modifying individual rows of the full table returned by autotest_package(). The following code demonstrates by showing how list-column tests can be switched off only for particular functions, starting again with the xf data of dummy tests generated above.

xf <- autotest_package (package = "stats",
                        functions = "cor")
xf$test [grepl ("list_col", xf$test_name) & xf$fn_name == "var"] <- FALSE
xt3 <- autotest_package (package = "stats",
                         functions = "cor",
                         test = TRUE,
                         test_data = xf)
xf <- autotest_package (package = "stats",
                        functions = "cor")
xf$test [grepl ("list_col", xf$test_name) & xf$fn_name == "var"] <- FALSE
xt3 <- autotest_package (package = "stats",
                         functions = "cor",
                         test = TRUE,
                         test_data = xf)
print (xt3)
print (xt3)

These procedures illustrate the three successively finer levels of control over tests, by switching them off for:

  1. Entire packages;
  2. Specified functions only; or
  3. Specific parameters of particular functions only.

4. autotest-ing your package

autotest can be very easily incorporated in your package's tests/ directory via to simple testthat expectations:

Using these requires adding autotest to the Suggests list of your package's DESCRIPTION file, along with testthat. Note that the use of testing frameworks other than testthat is possible through writing custom expectations for the output of autotest_package(), but that is not considered here.

To use these expectations, you must first decide which, if any, tests you judge to be not applicable to your package, and switch them off following the procedure described above (that is, at the package level through modifying the test flag of the object returned from autotest_types(), or at finer function- or parameter-levels by modifying equivalent values in the object returned from autotest_package(..., test = FALSE). These objects must then be passed as the test_data parameter to autotest_package(). If you consider all tests to be applicable, then autotest_package() can be called without specifying this parameter.

If you switch tests off via a test_data parameter, then the expect_autotest expectation requires you to append an additional column to the test_data object called "note" (case-insensitive), and include a note for each row which has test = FALSE explaining why those tests have been switched off. Lines in your test directory should look something like this:

library (testthat) # as called in your test suite
# For example, to switch off vector-to-list-column tests:
test_data <- autotest_types (notest = "vector_to_list_col")
test_data$note <- ""
test_data$note [test_data$test == "vector_to_list_col"] <-
    "These tests are not applicable because ..."
expect_success (expect_autotest_testdata (test_data))

This procedure of requiring an additional "note" column ensures that your own test suite will explicitly include all explanations of why you deem particular tests not to be applicable to your package.

In contrast, the following expectation should be used when autotest_package() passes with all tests are implemented, in which case no parameters need be passed to the expectation, and tests will confirm that no warnings or errors are generated.

expect_success (expect_autotest_no_testdata ())

4.2 Finer control over testing expectations

The two expectations shown above call the autotest_package() function internally, and assert that the results follow the expected pattern. There are also three additional testthat expectations which can be applied to pre-generated autotest objects, to allow for finer control over testing expectations. These are:

These tests are demonstrated in one of the testing files used in this package, which the following lines recreate to demonstrate the general process. The first two expectations are that an object be free from both warnings and errors. The tests implemented here are applied to the stats::cov() function, which actually triggers warnings because two parameters do not have their usage demonstrated in the example code. The tests therefore expect_failure(), when they generally should expect_success() throughout.

library (testthat) # as called in your test suite
# For example, to switch off vector-to-list-column tests:
test_data <- autotest_types (notest = "vector_to_list_col")
x <- autotest_package (package = "stats",
                       functions = "cov",
                       test = TRUE,
                       test_data = test_data)

expect_success (expect_autotest_no_err (x))
expect_failure (expect_autotest_no_warn (x)) # should expect_success!!
library (testthat) # as called in your test suite
# switch off vector-to-list-column tests:
test_data <- autotest_types (notest = "vector_to_list_col")
x <- autotest_package (package = "stats",
                       functions = "cov",
                       test = TRUE,
                       test_data = test_data)

expect_success (expect_autotest_no_err (x))
expect_failure (expect_autotest_no_warn (x)) # should expect_success!!

The test files then affirms that simply passing the object, x, which has tests flagged as type == "no_test", yet without explaining why in an additional "note" column, should cause expect_autotest() to fail. The following line, removing the logical testthat expectation, demonstrates:

expect_autotest_notes (x)
expect_autotest_notes (x)

As demonstrated above, these expect_autotest_... calls should always be wrapped in a direct testhat expectation of expect_success(). To achieve success in that case, we need to append an additional "note" column containing explanations of why each test has been switched off:

x$note <- ""
x [grep ("vector_to_list", x$test_name), "note"] <-
  "these tests have been switched off because ..."

expect_success (expect_autotest_notes (x))

In general, using autotest in a package's test suite should be as simple as adding autotest to Suggests, and wrapping either expect_autotest_no_testdata or expect_autotest_testdata in an expect_success call.



mpadge/autotest documentation built on Feb. 20, 2021, 8:40 a.m.