Paul Rougieux
Paul Rougieux

Reputation: 11379

How to test an installed package with testthat?

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?

Upvotes: 8

Views: 2530

Answers (2)

Steffen Moritz
Steffen Moritz

Reputation: 7730

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()

Upvotes: 5

Joshua Ulrich
Joshua Ulrich

Reputation: 176638

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.

Upvotes: 7

Related Questions