Skip to contents

The first tutorial checks that selecting samples preserves the requested order. A Bioconductor function may receive an ordinary matrix or an HDF5-backed assay. We want the same behavior from both, with one shared set of assertions.

This tutorial uses the GitHub development version, testthat 3.3.0 or newer, SummarizedExperiment and HDF5Array. See the first tutorial’s installation block. It starts with a fresh copy; no objects from another article are required.

Prepare the two cases

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.
writeLines("^\\.BBSoptions$", file.path(pkg, ".Rbuildignore"))

The copy command replaces the first tutorial’s @tests block with the following complete file. In your own package, you would edit the R source directly. use_longtests() creates the separate runner and enables RunLongTests: TRUE in .BBSoptions, preserving an existing runner. The final line keeps this build-system setting out of the example’s source tarball. In an existing package, append that pattern to .Rbuildignore instead of replacing its other entries.

#' Select samples in the requested order
#'
#' @param x A SummarizedExperiment.
#' @param samples Sample names in the order to return.
#' @return A SummarizedExperiment with the selected columns.
#' @export
#' @testCases sample-alignment
#' list(
#'   cases = list(
#'     memory = list(suite = "tests", setup = {
#'       x <- example_experiment()
#'       expect_true(is.matrix(SummarizedExperiment::assay(x)))
#'     }),
#'     hdf5 = list(suite = "longtests", setup = {
#'       x <- example_experiment()
#'       file <- withr::local_tempfile(fileext = ".h5")
#'       SummarizedExperiment::assay(x) <- HDF5Array::writeHDF5Array(
#'         SummarizedExperiment::assay(x), file, name = "counts", with.dimnames = TRUE
#'       )
#'       expect_s4_class(SummarizedExperiment::assay(x), "HDF5Matrix")
#'     })
#'   ),
#'   test = {
#'     selected <- align_samples(x, c("s3", "s1"))
#'     expect_identical(colnames(selected), c("s3", "s1"))
#'     expect_identical(as.integer(SummarizedExperiment::assay(selected)), c(31L, 11L))
#'     expect_identical(selected$condition, c("treated", "control"))
#'   }
#' )
align_samples <- function(x, samples) x[, samples]

Read the declaration in three parts:

  • sample-alignment names the group. Combined with each case name it gives the stable IDs sample-alignment/memory and sample-alignment/hdf5.
  • setup creates the input for each case. The HDF5 case writes a real backing file, checks its class and cleans it up after the test with withr.
  • test runs the same three assertions for each input. The expected sample names, counts and conditions are written explicitly.

The memory case belongs to the ordinary suite. The HDF5 case goes into longtests so it can run separately with its backend dependency. This tiny example is fast: HDF5 does not automatically make a test slow, and you can put both cases in the ordinary suite if that suits your package.

For a standalone long test with no shared assertions, use an @longtests block instead of @tests and enable the suite with use_longtests(). Named cases are useful when you want to reuse a test body or select a case by ID.

Generate and inspect the plan

roxygen2::roxygenise(pkg)
plan <- biocroxytest::test_case_plan(pkg)
plan[c("id", "suite")]
#>                        id     suite
#> 1   sample-alignment/hdf5 longtests
#> 2 sample-alignment/memory     tests

You should see two rows, one for each ID and its chosen suite. Planning reads the declarations without running the setup or creating an HDF5 file. Moving a case to another source file or suite does not change its ID.

The generated files are:

Case Generated file
memory tests/testthat/test-roxytest-tests-align-samples.R
hdf5 longtests/testthat/test-biocroxytest-longtests-align-samples.R

They run with standard testthat. From an R session you can run both source suites:

ordinary <- testthat::test_local(pkg, reporter = "silent")
long <- testthat::test_dir(file.path(pkg, "longtests", "testthat"),
                           package = "samplealignment", load_package = "source",
                           reporter = "silent")
rbind(ordinary = as.data.frame(ordinary)[c("failed", "error", "passed")],
      long = as.data.frame(long)[c("failed", "error", "passed")])
#>          failed error passed
#> ordinary      0 FALSE      4
#> long          0 FALSE      4

Each suite should pass four expectations: one for the input backend and three for the selected samples. For an installed package check, use R CMD check package_version.tar.gz and R CMD check --test-dir=longtests package_version.tar.gz in separate output directories. Each suite must have its own helpers; it must not rely on the other suite having run first.

Run just one case

During development, select the case you are working on:

biocroxytest::run_test_case("sample-alignment/hdf5", path = pkg)
#> biocroxytest-longtests-align-samples: ....
#> 
#> ══ DONE ════════════════════════════════════════════════════════════════════════
#> Way to go!

This runs the selected generated test with its suite’s helpers. Regenerate with roxygen2::roxygenise(pkg) whenever you change the case declaration. Use a full suite run for custom runners or tests that depend on earlier tests.

Record whether both cases completed

Use run_test_plan() when you want a structured report. It runs each case in its own R session and temporary package copy, so each result has an execution context. This is optional; ordinary testthat runs remain the basic workflow.

ids <- c("sample-alignment/memory", "sample-alignment/hdf5")
evidence <- biocroxytest::run_test_plan(pkg, ids = ids, seed = 42L)
evidence$cases[c("id", "suite", "status")]
#>                        id     suite    status
#> 1 sample-alignment/memory     tests completed
#> 2   sample-alignment/hdf5 longtests completed
biocroxytest::check_test_plan(evidence, required = ids)

Both statuses should be completed. A passing assertion followed by a skip does not count as complete. check_test_plan() requires both IDs to complete without recorded warnings, and raises an error otherwise. This can make a backend case mandatory in CI instead of silently accepting a skip.

Next: introduce, record and replay a sample-ordering failure. For specific tasks, see fixture files and CI, plan comparisons and source-copy rules.