-
Notifications
You must be signed in to change notification settings - Fork 289
Description
I work with a team of developers maintaining several R packages. While we encourage the use of usethis::use_test()
to create test files, some team members, particularly those new to software development, often ask what they should be testing. To support them, we created a helper function based on usethis::use_test()
that uses a test template with brief guidance. This template encourages test coverage in the following areas: Input/output correctness
, Edge cases
, and Built-in errors/warnings
.
Would the {usethis}
team consider expanding the inst/templates/test-example-2.1.R
to include similar high-level testing prompts? Please feel free to close this issue if you feel this is out of scope.
If it’s out of scope, do you have any recommendations for distributing a custom test template across multiple packages (e.g., develop an R package with these templates or standalone files)?
Below is an example of the customized template for reference.
# {{{ function_name }}} ----
## Setup ----
# Load or prepare any necessary data for testing
## IO correctness ----
test_that("{{{ function_name }}}() works with correct inputs", {
# Test that {{{ function_name }}}(x) returns y.
expect_equal(
object = {{{ function_name }}}(x),
expected = y
)
})
## Edge handling ----
# Please remove/comment out the test template below if no edge cases are being tested.
test_that("{{{ function_name }}}() returns correct outputs for edge cases", {
# Test that {{{ function_name }}}(x) returns an error.
expect_error(
object = {{{ function_name }}}(x)
)
})
## Error handling ----
# Please remove/comment out the test template below if there are no built-in errors/warnings.
test_that("{{{ function_name }}}() returns correct error messages", {
# Test that {{{ function_name }}}(x) returns expected error.
expect_error(
object = {{{ function_name }}}(x),
regexp = "Insert text here that should be in the error message."
)
})