Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test an installed package with testthat?

Tags:

r

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?

like image 517
Paul Rougieux Avatar asked Apr 12 '16 13:04

Paul Rougieux


People also ask

How do I run test with Testthat?

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.

How to do unit test in r?

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.

What is Testthat?

'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.


2 Answers

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.

like image 149
Joshua Ulrich Avatar answered Dec 10 '22 13:12

Joshua Ulrich


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()
like image 31
Steffen Moritz Avatar answered Dec 10 '22 15:12

Steffen Moritz