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.
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.