8

我正在使用该testthat包为我的 R 包编写测试。我已按照http://r-pkgs.had.co.nz/tests.html上的说明进行操作(我相信)。我用了

devtools::use_testthat()

设置测试骨架。我在其中创建了一个测试文件,tests/testthat文件名以test. 当我devtools::test()在 RStudio 中运行或 Ctrl+Shift+T 时,测试运行成功,但是当我运行R CMD check或 Ctrl+Shift+E 时,testthat找不到我的包。我得到错误

> library(testthat)
> 
> test_check("foo")
Loading required package: foo
Error in loadNamespace(name) : there is no package called 'foo'
Calls: test_check ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'foo'
Execution halted

我确实通过R_LIBS_SITE在我的.Renviron文件中设置来修改我的库路径。我怀疑这在运行时没有被读取R CMD check,但我认为它不应该有所作为。

当我devtools::check()从 RStudio 控制台运行时,它成功完成(包括测试),但是在 RStudio 中运行检查失败。

我添加了一些调试来testthat.R打印输出.libPaths()和其他位:

> library(testthat)
> .libPaths()
[1] "C:/Users/timk/AppData/Local/Temp/Rtmp841w0b/RLIBS_1790551706"
[2] "C:/Program Files/R/R-3.2.2/library"
> list.files(.libPaths()[1])
 [1] "KernSmooth" "MASS"       "Matrix"     "boot"       "class"
 [6] "cluster"    "crayon"     "digest"     "foo"        "foreign"
[11] "lattice"    "magrittr"   "memoise"    "mgcv"       "nlme"
[16] "nnet"       "praise"     "rpart"      "spatial"    "stringi"
[21] "stringr"    "survival"   "testthat"
> list.files(file.path(.libPaths()[1], "foo"))
character(0)
> list.files(file.path(.libPaths()[1], "testthat"))
 [1] "CITATION"    "DESCRIPTION" "INDEX"       "LICENSE"     "MD5"
 [6] "Meta"        "NAMESPACE"   "R"           "help"        "html"
[11] "libs"

可以看到在临时库中创建了包目录,但是包是空的。将其与testthat.

我还尝试下载另一个使用testthat匿名器)的软件包,但遇到了同样的错误。

sessionInfo()

R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

attached base packages:
 [1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] foo_0.1  testthat_0.11.0

loaded via a namespace (and not attached):
 [1] magrittr_1.5   tools_3.2.2    roxygen2_5.0.0 Rcpp_0.12.1    crayon_1.3.1   memoise_0.2.1  stringi_1.0-1 
 [8] stringr_1.0.0  digest_0.6.8   devtools_1.9.1
4

2 回答 2

1

尝试testthat在. Suggests:_ DESCRIPTIONR CMD CHECK 只会在检查期间将该文件中提到的包放入范围。

于 2016-01-22T16:25:42.723 回答
0

你似乎忘记了这条线

library(foo)

在你的 testthat 文件中。如果我看一些著名的例子,他们就是这样做的

于 2017-09-07T10:12:33.930 回答