Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a package's testthat tests

Tags:

r

testthat

For a given installed package, how do I run its testthat tests? I'm not a developer of the installed package, I'm a user. I just want to run its test suite to confirm its tests pass in my environment. I've tried test_check and test_package but I see errors.

To be more specific, I know its test suite fails and I want to run the tests in an interactive R session so I can debug it.

> require(eplusr)    # choice of package unimportant. Any using testthat will do.
Loading required package: eplusr
> require(testthat)
Loading required package: testthat
> test_check("eplusr")
Error in test_files(paths, reporter = reporter, env = env, stop_on_failure = stop_on_failure,  : 
  No matching test file in dir
> test_package("eplusr")
Error: No tests found for eplusr


> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8      
 [8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] testthat_2.0.0 eplusr_0.9.1  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18      magrittr_1.5      units_0.6-0       hms_0.4.2         progress_1.2.0    uuid_0.1-2        bit_1.1-14        debugme_1.1.0     R6_2.2.2          rlang_0.2.2      
[11] stringr_1.3.1     blob_1.1.1        tools_3.5.1       data.table_1.11.5 cli_1.0.0         DBI_1.0.0         withr_2.1.2       bit64_0.9-7       assertthat_0.2.0  digest_0.6.16    
[21] tibble_1.4.2      crayon_1.3.4      processx_3.2.0    readr_1.1.1       callr_3.0.0       later_0.7.4       base64enc_0.1-3   ps_1.1.0          fasttime_1.0-2    memoise_1.1.0    
[31] RSQLite_2.1.1     stringi_1.2.4     pillar_1.3.0      compiler_3.5.1    prettyunits_1.0.2 lubridate_1.7.4   pkgconfig_2.0.2  
like image 233
Matt Dowle Avatar asked Sep 04 '18 23:09

Matt Dowle


People also ask

How do I execute a single test from a package?

A single test method can be exuted using the following syntax All tests from subpackages may be includes as well, in order to execute all tests in or beneath package de.mypackage.sub execute: There are further possibilities like using regular expressions, see the official documentation of running a single test.

How to run all tests in a package in R?

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 do I create a test file in R?

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.

What is testthat in testthat?

testthat draws inspiration from the xUnit family of testing packages, as well as from many of the innovative ruby testing libraries, like rspec, testy, bacon and cucumber. testthat is the most popular unit testing package for R and is used by thousands of CRAN packages.


2 Answers

R doesn't install the testthat tests by default. To do so try :

install.packages('eplusr', INSTALL_opts="--install-tests", type='source')

Then, either:

testthat::test_package('eplusr')

or alternatively using the built-in testInstalledPackage:

tools::testInstalledPackage('eplusr')
like image 61
BrodieG Avatar answered Oct 21 '22 08:10

BrodieG


You can't (unless you reinstall overriding default behaviour as shown in Brodie's answer).

It's a design flaw^Hchoice. testthat, in all its wisdom, decided to not install tests by default by enforcing the placement in the tests/ directory. R offers an option to override it (as shown) which is generally not turned on. Hence the dilemma.

Of course, RUnit did it correctly all along having tests below inst/. So if a package uses RUnit you can run its tests once installed. Without having to reinstall it.

Edit: 1 1/2 years later, many of us now use the tinytest package which arrived within the last year. Among other advantages, also allows very easy testing of installed packages.

like image 32
Dirk Eddelbuettel Avatar answered Oct 21 '22 07:10

Dirk Eddelbuettel