[R] R CMD check and RUnit

From: Gorjanc Gregor <Gregor.Gorjanc_at_bfro.uni-lj.si>
Date: Sun 06 Aug 2006 - 09:36:14 EST


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:

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:

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.

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun Aug 06 11:15:47 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 10 Aug 2006 - 08:20:03 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.