vendredi 29 mai 2015

Running example(foo) via devtools::test() fails

I'm trying to use the example function within my unit tests (via testthat) in my package called apackage, say. However, example does not seem find the help page when I run devtools::test().

What I really want to accomplish is to use a function called foo in my test script test-bar.R. However, foo is defined in the example section for bar in apackage. Hence, i want to use example("bar") to make foo available in test-bar.R.

I'm aware the test_example and test_examples exist, but these do not make the functions within available in the test script.

Simplified, my test script test-bar.R looks something like:

context("Unit test of bar")

example("bar", package = "mypackage", character.only = TRUE) # defines foo
expect_that(exists("foo"), is_true()) 

Of course, I really mean to test is that foo(x) agrees with bar(x) in some sense.

My man/bar.Rd:

\name{bar}
[...]
\examples{
  foo <- function(x) {
    return(x)
  }
}

Running the tests fail, giving a warning:

devtools::test()
#Testing apackage
#Unit test of bar: 1
#
#1. Failure (at test-bar.R#4):  ------------------------------------------
#exists("foo") isn't true
#Warning message:
#In example("bar", package = "apackage", character.only = TRUE) :
#  no help found for ‘bar’

So it seems example can't find the help page. When I've loaded the package and testthat and source the test script, it works without problems. When I check the package, e.g. via devtools::check(document = FALSE, cleanup = FALSE), it also works without any problems.

How do I best accomplish this? Alternative "work-flows" are also welcome. I've could of course copy the function definition into the test-foo.R script also, but then I have to maintain it both places.

I use Rstudio. I might also note that bar is not an exported function. The documentation is marked internal.

Aucun commentaire:

Enregistrer un commentaire