Learn R Programming

patrick (version 0.0.2)

with_parameters_test_that: Execute a test with parameters.

Description

This function is an extension of [testthat::test_that()] that lets you pass a series of testing parameters. These values are substituted into your regular testing code block, making it reusable and reducing duplication.

Usage

with_parameters_test_that(desc_stub, code, .cases = NULL, ...)

cases(...)

Arguments

desc_stub

A string scalar. Used in creating the names of the parameterized tests.

code

Test code containing expectations.

.cases

A data frame where each row contains test parameters.

...

Named arguments of test parameters.

Details

You have a couple of options for passing parameters to you test. You can use named vectors/ lists. The function will assert that you have correct lengths before proceeding to test execution. Alternatively you can used a `data.frame` or list in combination with the splice unquote operator !!!. Last, you can use the constructor `cases()`, which is similar to building a `data.frame` rowwise. If you manually build the data frame, pass it in the `.cases` argument.

One parameter is noteworthy. If the user passes a character vector as `test_name`, each instance is combined with `desc_stub` to create the completed test name. Similarly, the named argument from `cases()` is combined with `desc_stub` to create the parameterized test names.

Examples

Run this code
# NOT RUN {
with_parameters_test_that("trigonometric functions match identities",
  {
    testthat::expect_equal(expr, numeric_value)
  },
  expr = c(sin(pi / 4), cos(pi / 4), tan(pi / 4)),
  numeric_value = c(1 / sqrt(2), 1 / sqrt(2), 1)
)

# Run the same test with the cases() constructor
with_parameters_test_that(
  "trigonometric functions match identities",
  {
    testthat::expect_equal(expr, numeric_value)
  },
  cases(
    sin = list(expr = sin(pi / 4), numeric_value = 1 / sqrt(2)),
    cos = list(expr = cos(pi / 4), numeric_value = 1 / sqrt(2)),
    tan = list(expr = tan(pi / 4), numeric_value = 1)
  )
)
# }

Run the code above in your browser using DataLab