# Test design in practice: builds a throwaway package whose tests use a helper # fixture, withr::local_tempdir() and local_options(), and # testthat::local_mocked_bindings() to freeze "today", then runs the tests. # # Rscript test-design-demo.R demo_files <- list( # A thin wrapper around the clock, so tests can replace it. "R/today.R" = c( "today <- function() Sys.Date()" ), "R/days-since.R" = c( "#' Whole days from a date until today", "#' @export", "days_since <- function(date) {", " as.integer(today() - as.Date(date))", "}" ), "R/report.R" = c( "#' Write a data frame to summary.csv in `dir`, returning the path", "#' @export", "write_summary <- function(data, dir) {", " path <- file.path(dir, \"summary.csv\")", " utils::write.csv(data, path, row.names = FALSE)", " path", "}", "", "#' Mean to one decimal place, using the session's decimal separator", "#' @export", "format_mean <- function(x) {", " format(round(mean(x), 1), nsmall = 1)", "}" ), # Helpers are sourced before the tests: fixtures live here. "tests/testthat/helper-fixtures.R" = c( "# The smallest data set the tests need.", "make_subjects <- function(n = 3) {", " data.frame(", " USUBJID = sprintf(\"01-701-%04d\", seq_len(n)),", " AGE = seq(60, by = 10, length.out = n)", " )", "}", "", "# A local_*() helper: a folder that is deleted when the calling test ends.", "local_output_dir <- function(env = parent.frame()) {", " withr::local_tempdir(\"output-\", .local_envir = env)", "}" ), "tests/testthat/test-days-since.R" = c( "test_that(\"days_since() counts whole days up to today\", {", " local_mocked_bindings(today = function() as.Date(\"2026-01-31\"))", " expect_equal(days_since(\"2026-01-01\"), 30L)", "})" ), "tests/testthat/test-report.R" = c( "test_that(\"write_summary() writes one row per subject\", {", " dir <- local_output_dir()", " path <- write_summary(make_subjects(), dir)", " expect_true(file.exists(path))", " expect_equal(nrow(utils::read.csv(path)), 3)", "})", "", "test_that(\"format_mean() follows the decimal separator option\", {", " withr::local_options(OutDec = \",\")", " expect_equal(format_mean(c(1, 2)), \"1,5\")", "})", "", "test_that(\"local_options() restores the option afterwards\", {", " with_comma <- function() {", " withr::local_options(OutDec = \",\")", " format_mean(c(1, 2))", " }", " expect_equal(with_comma(), \"1,5\")", " expect_equal(format_mean(c(1, 2)), \"1.5\")", "})" ) ) # Creates the package and returns its path. make_demo_package <- function(path = file.path(tempdir(), "trialclock")) { usethis::create_package(path, rstudio = FALSE, open = FALSE) usethis::with_project(path, { usethis::use_testthat(3) usethis::use_package("withr", type = "Suggests") }) for (file in names(demo_files)) { writeLines(demo_files[[file]], file.path(path, file)) } path } # Loads the package from source and runs its tests (like devtools::test()). run_demo_tests <- function(path, reporter = testthat::default_reporter()) { testthat::test_local(path, reporter = reporter, stop_on_failure = FALSE) } # Run only when called as a script (not when sourced by the tests). if (sys.nframe() == 0L) { path <- make_demo_package() results <- run_demo_tests(path) print(as.data.frame(results)[, c("file", "test", "nb", "failed")]) }