7

I would like to test an installed package, but this returns an error.

library(testthat)
test_package("testthat")
# Error: No tests found for testthat

test_package (source here) returns this error because system.file("tests", package = package) is empty. In fact, the tests directory is missing from the package installed.

list.dirs(system.file("", package = "testthat"))
# [1] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat/"     
# [2] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//help"
# [3] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//html"
# [4] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//libs"
# [5] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//Meta"
# [6] "/home/paul/R/x86_64-pc-linux-gnu-library/3.2/testthat//R"  

How to install a package so that its tests directory remains present?

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
  • 1
    https://stackoverflow.com/questions/31380593/include-tests-in-binary-r-package suggests a way to bundle tests into package WITHOUT placing in inst – lunguini Apr 26 '19 at 17:33

2 Answers2

7

If the author chooses not to put the tests in the inst/ directory, then they will not be installed with the package and you cannot run the tests via the installed package.

So there's nothing you can do, short of modifying the source package and re-installing. But at that point, you might as well just run the tests on the source package.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 1
    I see `inst/tests` is mentioned in [this other answer](http://stackoverflow.com/a/19287167/2641825). So it looks like if I want to disseminate tests with my source package, I have to place them in the inst/ directory. Why Hadley didn't mention this in [his package testing chapter](http://r-pkgs.had.co.nz/tests.html)? – Paul Rougieux Apr 12 '16 at 13:48
  • @PaulRougieux: I don't know why he didn't mention it; you could ask him. – Joshua Ulrich Apr 12 '16 at 13:52
  • My original intend was to share tests with my source package. I simply moved `tests` to `inst/tests`, rebuilt the package and now `testthat::test_package()` works. – Paul Rougieux Apr 12 '16 at 13:53
  • 2
    the answer by @stats0007 mentions an important option that is missing in this accepted answer: `install.packages("testthat", INSTALL_opts = "--install-tests")` – lunguini Apr 26 '19 at 17:42
4

You can test packages with

tools::testInstalledPackage("package")

But I think it just works only if the tests are in inst/

There is also

install.packages("testthat", INSTALL_opts = "--install-tests")

to install also the tests with the package. But also just works if test are in inst/

So probably best you download the source package and run:

devtools::test()
Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55