Skip to contents

Suppose a test fails on a colleague’s machine. A replay record identifies the case, source revision, fixture checks, R seed and observed environment. With the matching source and inputs, you can rerun the case and compare the failure. It does not bundle the source or install old R/package versions for you.

We will deliberately introduce the sample-ordering bug from the first tutorial, record it, replay it and fix it. This uses the named cases from the memory/HDF5 tutorial, but all setup is repeated here so the article can be run independently. Use the GitHub development version and the dependencies listed in the first tutorial.

Start from the example package

template <- system.file("examples", "samplealignment", package = "biocroxytest",
                        mustWork = TRUE)
pkg <- tempfile("samplealignment-")
fs::dir_copy(template, pkg)
source_file <- file.path(pkg, "R", "align-samples.R")
named_source <- system.file("examples", "steps", "named-cases.R",
                            package = "biocroxytest", mustWork = TRUE)
file.copy(named_source, source_file, overwrite = TRUE)
#> [1] TRUE
withr::with_dir(pkg, biocroxytest::use_longtests())
#> ✔ Created longtests/testthat.R.
#> ✔ Enabled long tests in .BBSoptions.

The case sample-alignment/memory asks for s3, s1 and expects counts 31, 11 and conditions treated, control. We will run only that case here.

Introduce a specific bug

The correct implementation selects columns by name:

align_samples <- function(x, samples) x[, samples]

Replace it with a membership check. %in% keeps columns in their original order, so requesting s3, s1 incorrectly returns s1, s3. The following block edits only that function line, preserving all the test assertions:

correct <- readLines(source_file)
broken <- correct
broken[startsWith(broken, "align_samples <-")] <-
  "align_samples <- function(x, samples) x[, colnames(x) %in% samples]"
writeLines(broken, source_file)

Run and record the failure

roxygen2::roxygenise(pkg)
id <- "sample-alignment/memory"
failed <- biocroxytest::run_test_plan(pkg, ids = id, seed = 42L)
failed$cases[c("id", "status")]
#>                        id status
#> 1 sample-alignment/memory failed
cat(failed$cases$detail, "\n")
#> Expected `colnames(selected)` to be identical to `c("s3", "s1")`.
#> Differences:
#> `actual`:   "s1" "s3"
#> `expected`: "s3" "s1"
#> 
#> Expected `as.integer(SummarizedExperiment::assay(selected))` to be identical to `c(31L, 11L)`.
#> Differences:
#>   `actual`: 11 31
#> `expected`: 31 11
#> 
#> Expected `selected$condition` to be identical to `c("treated", "control")`.
#> Differences:
#> `actual`:   "control" "treated"
#> `expected`: "treated" "control"
#> 

The status should be failed. The diagnostic identifies three mismatches: sample names, counts and conditions. The backend check still passes. run_test_plan() returns the report so you can inspect or export failures; use check_test_plan() when failures must stop a CI job.

Save and replay it before editing the source

record_file <- tempfile("alignment-failure-", fileext = ".rds")
record <- biocroxytest::write_test_replay(failed, id, record_file)
replayed <- biocroxytest::replay_test_case(record_file, path = pkg)
replayed$comparison$same_failure
#> [1] TRUE

The result should be TRUE: the failure’s condition types, messages and available source locations match. record$command provides a command for replaying from the source package’s root. The explicit path = pkg above lets us stay in our current working directory.

Keep the record outside the package directory, as we do here. Otherwise saving it could change the source fingerprint. Export requires a failed case and a new output filename. Keep the matching faulty source if you want to replay it again after fixing the package.

Replay restores the recorded base R seed and selected environment settings. Inspect replayed$comparison$environment_changes for differences. Matching R and dependency versions, external services and backend-specific random streams still need to be managed separately.

Fix the function and keep the regression test

Restore name-based selection and run the same case against the corrected source. This is a new test run: replay intentionally requires the original source revision.

writeLines(correct, source_file)
fixed <- biocroxytest::run_test_plan(pkg, ids = id, seed = 42L)
biocroxytest::check_test_plan(fixed, required = id)
fixed$cases[c("id", "status")]
#>                        id    status
#> 1 sample-alignment/memory completed

The case should now be completed, and check_test_plan() should return without an error. We changed only the function body, so the generated test assertions remain current. If you edit a declaration, regenerate it first. Commit the corrected source and its generated regression test.

You have now seen the same expected result protect sample order in an ordinary test, across two backends, and through failure replay. Continue with verified fixture files, CI recipes, or two historical Bioconductor regressions. The paired-data reduction recipe is an optional next step when the data needed to reproduce a failure are too large.