Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test that an error does not occur?

Tags:

r

testing

I am starting to implement testing for an R package, and have been using the testthat package. Note that I am new to testing, so perhaps my approach is off.

I have a function that currently fails the 16th time it is executed, and before fixing this, I want to write a regression test that will catch it if it re-appears.

e.g., the following always throws the same error message:

 for i in (1:17) myfun() 

myfun does not return anything, it only has a side-effect of opening a database connection. It is clear to me that I can write a test that expects an error and passes if it is returned:

 expect_error(for (i in 1:17) myfun())  

But I don't quite get how to write a test to ensure that the error does not occur. As it is not obvious, perhaps my approach is wrong. I can figure out how to write more specific tests, but I would like to start with this one.

What type of test would I write to make sure that such an error does not appear?

like image 999
David LeBauer Avatar asked May 31 '12 01:05

David LeBauer


1 Answers

Major edit due to changes in testthat

Since version 0.11 (via RStudio blog) there is direct support to testing lack of errors:

expect_error(myfun(), NA) 

Same for catching warning and message:

expect_warning(myfun(), NA) expect_message(myfun(), NA) 

Side note: There is an info parameter in expect_xxx functions to pass additional info. So you can do:

for (i in 1:17) expect_error(myfun(), NA, info = paste("i =", i)) 
like image 178
Marek Avatar answered Sep 23 '22 05:09

Marek