Friday 15 January 2010

unit testing - Printing custom diagnostic information if `testthat` test fails in `R` -



unit testing - Printing custom diagnostic information if `testthat` test fails in `R` -

i utilize testthat unit test check whether data.frame returned function identical 1 expect return. if test fails, testthat prints diagnostic information, instance:

myfunction(df.orig) not identical df.expected. differences: names: 1 string mismatch

however, see actual output of function, i.e. print returned data.frame. there way print output of tested function (or other custom diagnostic information) if testthat test fails?

indirectly, yes! if @ arguments expect_that, you'll see "info" parameter - can throw anonymous function or phone call in here that'll print results. so, like:

expect_that(df.orig, is_identical_to(df.expected), info = print(df.orig))

the disadvantage of /always/ print df.orig or similar information, if test passes.

the other way of doing (and thing ensure only triggers if error occurs) utilize trycatch; like:

trycatch( expr = { expect_that(df.orig, is_identical_to(df.expected)) }, error = function(e){ print(e) print(df.orig) #and else want happen when errors } )

this lot clunkier-looking, has couple of advantages - print df.orig if expect_that phone call produces error, can output...well, want, , can produce different results errors versus warnings. gnarly, however.

r unit-testing testthat

No comments:

Post a Comment