45

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?

0

4 Answers 4

50

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))
1
  • 1
    Please note well that this must be NA (i.e. logical NA), not NA_character_ Oct 23, 2018 at 9:45
10

Maybe wrap it with another expect_error.

Example:

expect_error(1)
expect_error(expect_error(1))
1
  • It does not seem to be working anymore (version 1.0.2) Mar 23, 2017 at 8:54
8

For example:

context("test error")
test_that("test error 1", {
  expect_true({log(10); TRUE})
})

test_that("test error 2", {
  expect_true({log("a"); TRUE})
})

will test if there is an error.

> test_file("x.r")
test error : .1


    1. Error: test error 2 -------------------------
    Non-numeric argument to mathematical function
    1: expect_true({
           log("a")
        TRUE
    })
    2: expect_that(object, is_true(), info, label)
    3: condition(object)
    4: expectation(identical(x, TRUE), "isn't true")
    5: identical(x, TRUE)

this means the first part passed the test while the second part failed.

5
  • thanks, but I am not familiar with the use of the curly braces and semi-colon inside the expectation (e.g. expect_true({...;...})) could you please explain? May 31, 2012 at 2:30
  • You can put any expression there. And the {...; ...; ...} is just an expression. So in you case, it should be written as expect_true({for (i in 1:17) {myfun()}; TRUE}).
    – kohske
    May 31, 2012 at 2:33
  • So this is basically trying to catch an error, since an error would prevent the second part of the expression (in this case, TRUE) from being evaluated? May 31, 2012 at 2:37
  • Yes, exactly. TRUE can be replaced by any value.
    – kohske
    May 31, 2012 at 2:38
  • I like the simplicity now that I understand May 31, 2012 at 4:22
4

Here is one solution using the expectation that tryCatch returns 0 when the error does not occur:

expect_equal(tryCatch(for(i in 1:17) myfun()), 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.