Skip to contents

Declare a group with @testCases group-name and a literal R list containing cases and a shared test block. Each named case has a suite of "tests" or "longtests" and can have a setup block. The generator puts the setup and shared body in the same test_that() call for each case. Neither block runs when planning or generating tests. Cases may also declare test_fixtures.

Usage

test_case_plan(path = ".")

Arguments

path

Path to the source package root.

Value

A data frame with case id, suite, relative source and output path, testthat description, generated code, and a fixture list column with the fixture declarations. Each row describes one named case declared directly in the package's roxygen source.

Details

Group names are unique in the package, and case names are unique within their group. Names start with an ASCII letter or digit and may also contain dots, underscores and hyphens. IDs have the form group/case and stay the same when a declaration moves to another source file or suite. Write the tag directly in the source. Use tests_roclet() to generate both suites.

Examples

pkg <- tempfile("test-case-example-")
dir.create(file.path(pkg, "R"), recursive = TRUE)
writeLines("Package: examplepkg", file.path(pkg, "DESCRIPTION"))
writeLines(c(
  "#' @testCases arithmetic",
  "#' list(",
  "#'   cases = list(small = list(suite = 'tests', setup = { x <- 2L })),",
  "#'   test = { expect_identical(x + 1L, 3L) }",
  "#' )",
  "NULL"
), file.path(pkg, "R", "example.R"))
test_case_plan(pkg)
#>                 id suite      source
#> 1 arithmetic/small tests R/example.R
#>                                           path      description
#> 1 tests/testthat/test-roxytest-tests-example.R arithmetic/small
#>                                                                             code
#> 1 test_that("arithmetic/small", {\n  x <- 2L\n  expect_identical(x + 1L, 3L)\n})
#>   fixture
#> 1        
unlink(pkg, recursive = TRUE)