[R] R CMD check and RUnit

Gorjanc Gregor Gregor.Gorjanc at bfro.uni-lj.si
Sun Aug 6 01:36:14 CEST 2006


I appologize for crossposting, but this might be of broader interest.

In case you are interested in fusing RUnit with R CMD check under unix
alike OS, here is one way of doing/hacking this.

My aim was to perform unit tests:
(1) during R CMD check
(2) at any other time

Say you have a package PKG in a map PKG. I use the following structure

 |- R
 |- ...
 |- inst
 |   |- doc
 |   `- unitTests
 |- ...
 `- tests

Then the following files (content is at the end of the mail) 
are used:
 - PKG/tests/doRUnit.R
 - PKG/inst/unitTests/Makefile
 - PKG/inst/unitTests/runit.*.R

Now when I launch 

cd /some/path/PKG/..
R CMD check PKG 

doRUnit.R in map tests is sourced and all unit tests in 
PKG/inst/unitTests are issued - aim (1). Output of tests can be 
seen in either:
  - PKG.Rcheck/tests/doRUnit.Rout
  - PKG.Rcheck/PKG/unitTests/report.txt
  - PKG.Rcheck/PKG/unitTests/report.html

R CMD check will say OK also when some unit tests fails, but
unit tests are at least issued and one should check above outputs
to see the results of unit testing. I use the following in my
~/.Makefile to see these results imidiately

NAME := $(shell basename $(PWD)) # Get current map name

Rcheck: # Check R package of current map
	(cd ..; \
	R CMD check $(NAME) && \
	if [ -d $(NAME)/inst/unitTests ]; then \
	  cat $(NAME).Rcheck/tests/doRUnit.Rout; \

Then only the following is necesarry to start R CME check and
print results of unit testing (if it is used)

mymake Rcheck

where mymake is a shell function

mymake ()
    make -f ~/.Makefile $*
export -f mymake


If one wants to run only unit tests - aim (2) against currently 
installed code, use

cd PKG/inst/unitTests
make test

or the following if package should be first re-installed, due
to changes

cd PKG/inst/unitTests


Please note that this hack is not only my work as I have taken 
the structure and initial idea from package graph in BioC.


Content of above mentioned files:

if(require("RUnit", quietly=TRUE)) {

  ## --- Setup ---

  testDir <- "unitTests"
  path <- "../PKG"
  ## Path for standalone i.e. not by R CMD check testing
  if(Sys.getenv("RCMDCHECK") == "FALSE") path <- "../inst"
  path <- file.path(getwd(), path, testDir)
  pathReport <- file.path(path, "report")

  ## --- Testing ---

  ## Define tests
  testsuite.PKG <- defineTestSuite(name="PKG unit testing",
  ## Run
  tests <- runTestSuite(testsuite.PKG)

  ## Print results
  printTextProtocol(tests, fileName=paste(pathReport, ".txt", sep=""))

  ## Print HTML version to a file
  printHTMLProtocol(tests, fileName=paste(pathReport, ".html", sep=""))


all: inst test

inst: # Install package
        cd ${TOP};\
        R CMD INSTALL ${PKG}

test: # Run unit tests
        export RCMDCHECK=FALSE;\
        cd ${TOP}/${PKG}/tests;\
        R --slave < ${SUITE}

# As many files with RUnit testing as you want

Lep pozdrav / With regards,
    Gregor Gorjanc

University of Ljubljana     PhD student
Biotechnical Faculty        URI: http://www.bfro.uni-lj.si/MR/ggorjan
Zootechnical Department     mail: gregor.gorjanc <at> bfro.uni-lj.si
Groblje 3                   tel: +386 (0)1 72 17 861
SI-1230 Domzale             fax: +386 (0)1 72 17 888
Slovenia, Europe
"One must learn by doing the thing; for though you think you know it,
 you have no certainty until you try." Sophocles ~ 450 B.C.

More information about the R-help mailing list