9

I am using the testthat package to write tests for my R package. I have followed the instructions at http://r-pkgs.had.co.nz/tests.html (I believe). I used

devtools::use_testthat()

to set up the testing skeleton. I have created a test file in tests/testthat and the filename begins with test. When I run devtools::test() or Ctrl+Shift+T in RStudio, the tests run successfully, however when I run R CMD check or Ctrl+Shift+E, testthat cannot find my package. I get the error

> 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

I do modify my library path by setting R_LIBS_SITE in my .Renviron file. I suspect that this is not being read when running R CMD check, but I don't think that it should make a difference.

When I run devtools::check() from the RStudio Console, it completes successfully (including tests), however running Check in RStudio fails.

I added some debugging to testthat.R to print out .libPaths() and other bits:

> 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"

You can see that the package directory is created in the temporary library, however the package is empty. Compare it with the file list for testthat.

I have also tried downloading another package that uses testthat (anonymizer) and am getting the same error.

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
Tim Keighley
  • 313
  • 2
  • 8
  • A few days ago I was running a check on a branch of `adegenet` and all hell broke lose. After adding `R_LIBS_USER` to the user environment path, things picked up. What does your `.libPaths()` say? – Roman Luštrik Nov 20 '15 at 08:10
  • A good question. I have added output to show the `.libPaths()` and the contents of the temporary library. The package directory is being created, but it is empty. – Tim Keighley Nov 22 '15 at 23:36
  • Have you tried adding `R_LIB_USER` to the user environment or removing the first (temporary) folder? – Roman Luštrik Nov 23 '15 at 11:41
  • 1
    I tried adding `R_LIBS_USER` and `R_LIBS_SITE` but neither got passed down to the check. I think that the most odd thing is the if I call `devtools::check()` from the console it works (including running the tests), however if I use the RStudio Check Package, it doesn't. I cannot see the difference between these calls. – Tim Keighley Nov 24 '15 at 00:41
  • hi Tim, 5 years later I have the same weird error when checking my package (after tons of times it passed correctly). Any idea what was the problem? – AleRuete May 20 '20 at 09:21
  • Sorry @AleRuete, I never figured out what was going wrong. It was only on that particular machine. If I tried the same package on a different machine it was fine. I hope that you have more luck. – Tim Keighley May 21 '20 at 12:33
  • Hi @tim-keighley, yes. It seems to be package and machine specific. It happened when I moved the content to another Github repository. Thanks for posting the initial question anyway. – AleRuete May 22 '20 at 18:30

2 Answers2

2

Try adding testthat in the Suggests: field of DESCRIPTION. R CMD CHECK will only put in scope the packages mentioned in that file during a check.

Lionel Henry
  • 6,652
  • 27
  • 33
  • 1
    `testthat` is in the `DESCRIPTION`. I think that R is able to find `testthat` but not able to find my package, but only when I use the RStudio shortcut. If I run it "by hand", the tests are found and run. – Tim Keighley Feb 01 '16 at 02:50
  • Then it's probably an RStudio bug which should be filed on their github. – Lionel Henry Feb 08 '16 at 09:47
0

You seem to forget the line

library(foo)

In your testthat file. If I look at some renowned examples, they do it this way

takje
  • 2,630
  • 28
  • 47