[R] Unit Testing Frameworks: summary and brief discussion

Hans-Peter gchappi at gmail.com
Thu May 10 07:48:39 CEST 2007


> - My code gives error and warning messages in some situations. I want to
> test that the errors and warnings work, but these flags are the correct
> response to the test. In fact, it is an error if I don't get the flag.
> How easy is it to set up automatic tests to check warning and error
> messages work?

Maybe like this:

### for errors:
res1fkt <- function() xls.info( exc )
res1 <- try( res1fkt(), silent = TRUE )
if (class( res1 ) != "try-error") stop( "xls.info, data not equal" )
cat( "REQUIRED (EXPECTED) error message: ", res1  )

### for warnings:
tryCatch( res1 <- encodeDateTime( yd, md, dd, hd, mind, secd, msd ),
    warning = function(x) cat("REQUIRED (EXPECTED) warning
message:\n", x$message, "\n" ) )
  # have to resubmit the command as I didn't find a way to execute the command
  # (assignement) and catch the warning message (but suppress the warning)
suppressWarnings( res1 <- encodeDateTime( yd, md, dd, hd, mind, secd, msd ) )
if (!all( res1 == ddate )) stop( "encode/decode , data not equal" )


-- 
Regards,
Hans-Peter



More information about the R-help mailing list