[R] Unit Testing Frameworks: summary and brief discussion

ml-r-help at epigenomics.com ml-r-help at epigenomics.com
Thu May 10 11:36:25 CEST 2007


Martin Morgan wrote:
> Oops, taking a look at the unit tests in RUnit, I see that specifying
> 'where=.GlobalEnv' is what I had been missing.
> 
> testCreateClass <- function() {
>     setClass("A", contains="numeric", where=.GlobalEnv)
>     a=new("A")
>     checkTrue(validObject(a))
>     removeClass("A", where=.GlobalEnv)
>     checkException(new("A"))
> }
>
this is necessary as the test code file is sourced and thus evaluated 
from the test runner residing inside RUnits namespace. Thus the setClass 
call is evaluated inside RUnits namespace which will fail unless you 
specify the call to take place in .GlobalEnv.

one comment to our example: it has paid off to use
on.exit(removeClass("A", where=.GlobalEnv))
and likewise for removeMethod
to ensure a failing test case will leave no temporary class definition 
around. Otherwise this could side effect on subsequent test cases and 
you'd be looking for errors in the wrong place. Currently RUnit does try 
to preserve the workspace as it is before each test case execution - but 
this does not hold for S4 classes and methods defined in test cases.

Regards,

   Matthias

> Executing test function testCreateClass  ...  done successfully.
> 
> RUNIT TEST PROTOCOL -- Wed May  9 11:11:27 2007 
> *********************************************** 
> Number of test functions: 1 
> Number of errors: 0 
> Number of failures: 0 
> 
> Sorry for the noise. Martin
> 
> Martin Morgan <mtmorgan at fhcrc.org> writes:
> 
>> ml-r-help at epigenomics.com writes:
>>
>>> anthony.rossini at novartis.com wrote:
>>> [...]
>>>> ============= From Seth Falcon:
>>>>   1. At last check, you cannot create classes in unit test code and
>>>>      this makes it difficult to test some types of functionality.  I'm
>>>>      really not sure to what extent this is RUnit's fault as opposed
>>>>      to limitation of the S4 implemenation in R.
>>> I'd be very interested to hear what problems you experienced. If you 
>>> have any example ready I'd be happy to take a look at it.
>>> So far we have not observed (severe) problems to create S4 classes and 
>>> test them in unit test code. We actually use RUnit mainly on S4 classes 
>>> and methods. There are even some very simple checks in RUnits own test 
>>> cases which create and use S4 classes. For example in tests/runitRunit.r
>>> in the source package.
>> RUnit has been great for me, helping to develop a more rigorous
>> programming approach and gaining confidence that my refactoring
>> doesn't (unintentionally) break the established contract.
>>
>> One of the strengths of unit tests -- reproducible and expressible in
>> the way that language sometimes is not:
>>
>> testCreateClass <- function() {
>>     setClass("A", contains="numeric")
>>     checkTrue(TRUE)
>> }
>>
>>
>> RUNIT TEST PROTOCOL -- Wed May  9 10:36:53 2007 
>> *********************************************** 
>> Number of test functions: 1 
>> Number of errors: 1 
>> Number of failures: 0 
>>
>>  
>> 1 Test Suite : 
>> CreateClass_test - 1 test function, 1 error, 0 failures
>> ERROR in testCreateClass: Error in assign(mname, def, where) : cannot add bindings to a locked environment
>>
>>> sessionInfo()
>> R version 2.6.0 Under development (unstable) (2007-05-07 r41468) 
>> x86_64-unknown-linux-gnu 
>>
>> locale:
>> LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
>>
>> attached base packages:
>> [1] "tools"     "stats"     "graphics"  "grDevices" "utils"     "datasets" 
>> [7] "methods"   "base"     
>>
>> other attached packages:
>>    RUnit
>> "0.4.15"
>>
>> -- 
>> Martin Morgan
>> Bioconductor / Computational Biology
>> http://bioconductor.org
>>
>> ______________________________________________
>> R-help at 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.
> 


-- 
Matthias Burger                     Project Manager/ Biostatistician
Epigenomics AG    Kleine Praesidentenstr. 1    10178 Berlin, Germany
phone:+49-30-24345-371                          fax:+49-30-24345-555
http://www.epigenomics.com           matthias.burger at epigenomics.com
--
Epigenomics AG Berlin           Amtsgericht Charlottenburg HRB 75861
Vorstand:   Geert Nygaard (CEO/Vorsitzender),  Dr. Kurt Berlin (CSO)
               Oliver Schacht PhD (CFO),  Christian Piepenbrock (COO)
Aufsichtsrat:   Prof. Dr. Dr. hc. Rolf Krebs (Chairman/Vorsitzender)



More information about the R-help mailing list