testhat: Including code inside `test_that()`

Hi Community Members and Experts,

I notice that people doing this with test_that()

test_that("A test", {
  # Some code
  # some expect_* calls
})

However, I usually do this:

# A lot of code to do something, such as generating the expected results
test_that("A test", {
  # Only expect_* calls
})

What are the pros and cons of these two approaches? I adopt the second approach mainly because, when testing a function, I usually use a different way to do the same thing, and see whether this way and the function agree. If I include this code inside test_that(), it will be very long. Howevever, given the popularity of the first approach, I would like to see whether I should switch to that one.

Thanks a lot.

-- Shu Fai

Short answer: don't do that.

Long answer: code inside test_that() is treated differently in several ways, here is some of them:

  • an error within test_that() is a test failure, and error outside of test_that() aborts the whole test file.
  • test_that() creates a standardized environment, e.g. sets the terminal width and capabilities. Code outside of test_that() does not benefit from this.
  • You can use skip() & co. inside test_that(), but not outside.

There are probably other reasons, and there'll be other reasons in the future, I am sure.

If you want to make your test_that() blocks shorter, write some functions, and call them from within test_that(). If you don't want to put these functions into your package code, you can put them into a tests/testthat/helper.R file, so they are only available for testing.

1 Like

Thanks a lot for the detailed explanation and suggestions! I am not aware that I can use helper.R for that purpose. I will try it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.