Skip to content

R package tests

4 messages · Nathan S. Watson-Haigh, Philippe GROSJEAN, robin hankin +1 more

#
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I was wondering if anyone could point me in the right direction for reading up on writing tests in
R. I'm writing some functions for inclusion into a package and would like to test them to ensure
they're doing what I expect them to do.

Are these approaches used for testing packages in CRAN?

Cheers,
Nathan


- --
- --------------------------------------------------------
Dr. Nathan S. Watson-Haigh
OCE Post Doctoral Fellow
CSIRO Livestock Industries
Queensland Bioscience Precinct
St Lucia, QLD 4067
Australia

Tel: +61 (0)7 3214 2922
Fax: +61 (0)7 3214 2900
Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html
- --------------------------------------------------------

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkluio8ACgkQ9gTv6QYzVL5X9QCgwvg5xjwZW2A2Z5G41iADu1Kz
hIkAoI5ISuAtHyQ+JwJSRBAc9q/oyeEt
=cqm4
-----END PGP SIGNATURE-----
#
There is a mechanism for testing code in R packages (R CMD check), see 
the Writing R extensions manual. If you need more flexibility for your 
tests, you could look at RUnit on CRAN, or svUnit on R-Forge 
(http://r-forge.r-project.org, on CRAN soon). For the later one, you 
install it by:

install.packages("svUnit", repos="http://R-Forge.R-project.org")

These is a vignette associated with svUnit:

vignette("svUnit")

Note that RUnit and svUnit are "test suite code" compatible, but they 
use very different mechanisms internally.
Best,

Philippe Grosjean

..............................................<?}))><........
  ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
  ) ) ) ) )
( ( ( ( (    Numerical Ecology of Aquatic Systems
  ) ) ) ) )   Mons-Hainaut University, Belgium
( ( ( ( (
..............................................................
Nathan S. Watson-Haigh wrote:
#
I think the OP was asking about test suites that test the software.

The R package structure includes a test/ directory which you can use
to put tests.

For example, in the onion package I check that I have got my
signs and multiplication table correctly implemented:

  stopifnot(Hi*Hj ==  Hk)
  stopifnot(Hj*Hi == -Hk)
  stopifnot(Hj*Hk ==  Hi)
  stopifnot(Hk*Hj == -Hi)
  stopifnot(Hk*Hi ==  Hj)
  stopifnot(Hi*Hk == -Hj)

[and a whole lot of others]

and the elliptic package includes a whole bunch of code
that verifies identities that appear in AMS-55.  It also
includes numerical  verification that the functions,
using randomish arguments, match the output of
mathematica or maple.
Philippe Grosjean wrote:
#
Hi Nathan,

in addition to what others have already mentioned there is some documentation
in the Writing R Extensions Manual:
- on the supported structure of packages, indicating where test code might be added
 http://stat.ethz.ch/R-manual/R-patched/doc/manual/R-exts.html#Package-subdirectories
- and the recommended standard approach to check a package
http://stat.ethz.ch/R-manual/R-patched/doc/manual/R-exts.html#Checking-and-building-packages

and a wiki article on how to combine the standard check (viz R CMD check) with any of the
unit testing approaches
http://wiki.r-project.org/rwiki/doku.php?id=developers:runit&s=unit%20test

Examples for the standard approach employed by R can be found
in the R source repository
https://svn.r-project.org/R/trunk/src/library/stats/tests/

and for unit test based checking
e.g. Rmetrics
http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/?root=rmetrics

or BioConductor examples
https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/Biobase/inst/UnitTests/
where you need the access info provided here
http://wiki.fhcrc.org/bioc/DeveloperPage


Regards,

 Matthias
Nathan S. Watson-Haigh wrote:
______________________________________________
R-help at r-project.org 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.