Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run R package tests without building or installing the package?

R CMD check automatically runs tests located in tests/ directory. However running the tests this way requires building the package first. After that R CMD check goes through various different sanity checks before finally reaching the tests at the end.

Question: Is there a way to run those tests without having to build or install the package first?

NOTE: without using testthat or other non-standard packages.

like image 937
Karolis Koncevičius Avatar asked Sep 03 '25 04:09

Karolis Koncevičius


2 Answers

To summarise our discussion.

  1. To my knowledge there is no standard alternative to R CMD check for unit testing provided by base R
  2. Typically for unit testing, I source everything under R/ (and dyn.load everything under source/) and then source everything under tests/ (actually, I also use the Example sections of the help pages in the man/ directory as test cases and compare their outcome to those from previous package versions)

I assume that these are the basic testing functionalities provided by devtools and testthat. If you expect to develop multiple packages and want to stay independent from non-base-R, I'd recommed to automate the above processes with custom scripts/packages.

I'd recomment looking into http://r-pkgs.had.co.nz/tests.html.

like image 53
Tom Avatar answered Sep 05 '25 01:09

Tom


The "vanilla" way to test a package without

  • running R CMD check
  • destroying an existing installation
  • cluttering your working directory

is to install into a temporary directory tmp.lib and test that installation in a second temporary directory tmp.out using tools::testInstalledPackage:

pkg.name <- "Matrix"
pkg.root <- normalizePath(file.path("path", "to", pkg.name), mustWork = TRUE) # an absolute path (?)

tmp.lib <- tempfile()
tmp.out <- tempfile()
dir.create(tmp.lib)
dir.create(tmp.out)
owd <- setwd(tmp.out)

tools::Rcmd(c("build", pkg.root))
* checking for file '/x/y/z/path/to/Matrix/DESCRIPTION' ... OK
.
.
.
* building 'Matrix_1.5-5.tar.gz'
list.files(tmp.out)
[1] "Matrix_1.5-5.tar.gz"
tools::Rcmd(c("INSTALL", "-l", tmp.lib, "--install-tests", "*.tar.gz"))
* installing *source* package 'Matrix' ...
.
.
.
* DONE (Matrix)
list.files(tmp.lib)
[1] "Matrix"
tools::testInstalledPackage(pkg.name, lib.loc = tmp.lib, types = "tests")
Running specific tests for package 'Matrix'
  Running 'Class+Meth.R'
  Running 'Simple.R'
  Running 'abIndex-tsts.R'
  Running 'base-matrix-fun.R'
  Running 'bind.R'
  comparing 'bind.Rout' to 'bind.Rout.save' ... OK
  Running 'dg_Matrix.R'
  Running 'dpo-test.R'
  Running 'dtpMatrix.R'
  Running 'factorizing.R'
  Running 'group-methods.R'
  Running 'indexing.R'
  comparing 'indexing.Rout' to 'indexing.Rout.save' ... OK
  Running 'matprod.R'
  Running 'matr-exp.R'
  Running 'other-pkgs.R'
  Running 'packed-unpacked.R'
  Running 'spModel.matrix.R'
  Running 'symmDN.R'
  Running 'validObj.R'
  Running 'write-read.R'
list.files(tmp.out, recursive = TRUE)
 [1] "Matrix-tests/Class+Meth.R"         "Matrix-tests/Class+Meth.Rout"     
 [3] "Matrix-tests/Rplots.pdf"           "Matrix-tests/Simple.R"            
 [5] "Matrix-tests/Simple.Rout"          "Matrix-tests/abIndex-tsts.R"      
 [7] "Matrix-tests/abIndex-tsts.Rout"    "Matrix-tests/base-matrix-fun.R"   
 [9] "Matrix-tests/base-matrix-fun.Rout" "Matrix-tests/bind.R"              
[11] "Matrix-tests/bind.Rout"            "Matrix-tests/bind.Rout.save"      
[13] "Matrix-tests/dg_Matrix.R"          "Matrix-tests/dg_Matrix.Rout"      
[15] "Matrix-tests/dpo-test.R"           "Matrix-tests/dpo-test.Rout"       
[17] "Matrix-tests/dtpMatrix.R"          "Matrix-tests/dtpMatrix.Rout"      
[19] "Matrix-tests/factorizing.R"        "Matrix-tests/factorizing.Rout"    
[21] "Matrix-tests/group-methods.R"      "Matrix-tests/group-methods.Rout"  
[23] "Matrix-tests/indexing.R"           "Matrix-tests/indexing.Rout"       
[25] "Matrix-tests/indexing.Rout.save"   "Matrix-tests/matprod.R"           
[27] "Matrix-tests/matprod.Rout"         "Matrix-tests/matr-exp.R"          
[29] "Matrix-tests/matr-exp.Rout"        "Matrix-tests/mm-Matrix.tab"       
[31] "Matrix-tests/other-pkgs.R"         "Matrix-tests/other-pkgs.Rout"     
[33] "Matrix-tests/packed-unpacked.R"    "Matrix-tests/packed-unpacked.Rout"
[35] "Matrix-tests/spModel.matrix.R"     "Matrix-tests/spModel.matrix.Rout" 
[37] "Matrix-tests/symmDN.R"             "Matrix-tests/symmDN.Rout"         
[39] "Matrix-tests/validObj.R"           "Matrix-tests/validObj.Rout"       
[41] "Matrix-tests/write-read.R"         "Matrix-tests/write-read.Rout"     
[43] "Matrix_1.5-5.tar.gz"
setwd(owd)
unlink(tmp.out)
unlink(tmp.lib)

Of course, you would want to put all of that into a function, ideally with an option to not destroy the output directory so that you can inspect the .Rout files and any .Rout.fail files as needed.

like image 34
Mikael Jagan Avatar answered Sep 05 '25 01:09

Mikael Jagan