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?
The easiest way to get started is with usethis. Assuming you're in a package directory, just run usethis::use_test("name") to create a test file, and set up all the other infrastructure you need. If you're using RStudio, press Cmd/Ctrl + Shift + T (or run devtools::test() if not) to run all the tests in a package.
Create a directory called tests and put there one ore more R scripts all starting with test_ as a file name. After this you could just start the unit testing code by calling testthat::test_dir(“tests”) within R and you will see an output similar to that. The output is shown after calling the tests.
'testthat' is a testing framework for R that is easy to learn and use, and integrates with your existing 'workflow'. License MIT + file LICENSE. URL https://testthat.r-lib.org, https://github.com/r-lib/testthat.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With