Exporting test packages using 'testthat'

Question: How can I get testthatto run in an environment that loads my package, and does not inherit from my package?

Background: A package testthatruns tests "in an environment that inherits from the package namespace environment" [see docs for test_check ]. This means that it does not guarantee that I performed my export correctly, and this bit me several times.

For example, I have the following code in my package:

##' The foo() method
##' @param x object
##' @export
foo <- function(x)
  UseMethod('foo')

##' @rdname foo
foo.data.frame <- function(x) {
  message("foo data.frame")
}

##' @rdname foo
foo.default <- function(x) {
  message("foo default")
}

And in my tests the following:

x <- 5:13
foo(x)

This is just great. But if the user installs the package, he will get this error:

Error in UseMethod("foo") : 
  no applicable method for 'foo' applied to an object of class "c('integer', 'numeric')"

The solution is to place declarations @exportsfor the two methods, but it is a bummer that the tests did not catch it.

, . , testthat:::run_tests , , ?

+4
1

test_dir. test_check . "/tests/run-tests.R" ( , ".R" ):

library(testthat)
library(<my package>)  # insert actual package name here
test_dir('testthat')   # assuming your tests are in "tests/testthat"

, :

setwd("<pkg dir>/tests")
source("run-tests.R")

:

cd <pkg-dir>/tests
Rscript run-tests.R

R CMD build R CMD check, .

setwd , . , , , R CMD check.

+1

Source: https://habr.com/ru/post/1612928/


All Articles