Skip to contents

biocroxytest is for developers of R packages. Write testthat checks beside an R function, then let roxygen2 generate ordinary test files. You keep using testthat to run the tests. You can add a separate Bioconductor long-test suite later.

This tutorial starts with a prepared package, so you can try the workflow without configuring your own project. It uses the GitHub development version of biocroxytest; the Bioconductor release has a separate release guide.

Install the tutorial dependencies

Run this once in R. Use R 4.4 or newer and a compatible Bioconductor release. The optional HDF5 dependency is used in the next tutorial.

if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager")
if (!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")
BiocManager::install(c("SummarizedExperiment", "HDF5Array", "testthat"), update = FALSE)
BiocManager::install("xec-cm/biocroxytest@devel", update = FALSE)

Copy the example package

Run the following blocks in order in an R session. All operations use pkg explicitly, so you do not need to change the working directory. The copy lives in a temporary directory and can be discarded after the tutorial.

template <- system.file("examples", "samplealignment", package = "biocroxytest",
                        mustWork = TRUE)
pkg <- tempfile("samplealignment-")
fs::dir_copy(template, pkg)

The important files in the copy are:

samplealignment/
  DESCRIPTION                 Package dependencies and roxygen configuration
  R/align-samples.R            Function and its test declaration
  R/example-experiment.R      Three samples with known measurements
  tests/testthat.R             Standard testthat runner

You can also inspect the complete example package on GitHub. To keep your work, use a new persistent directory instead of tempfile().

Read the function and its test

Our function selects samples from a SummarizedExperiment. Asking for s3 and then s1 must return both measurements and metadata in that order. The input contains one gene:

Sample Count Condition
s1 11 control
s2 21 control
s3 31 treated

Open R/align-samples.R in the copy. Its complete contents are:

#' 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
#' @tests
#' x <- example_experiment()
#' 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]

example_experiment() constructs the small object above. The three expect_identical() calls check sample order, counts and conditions against explicit expected values. @tests tells biocroxytest to put these checks in an ordinary test. A roclet is the roxygen2 extension that does that writing.

Generate the test

The example’s DESCRIPTION already contains this configuration:

Roxygen: list(roclets = c("namespace", "rd", "biocroxytest::tests_roclet"))

The bundled package is already configured. Generate its test and refresh the function documentation together:

roxygen2::roxygenise(pkg)

The test is written to tests/testthat/test-roxytest-tests-align-samples.R. It is an ordinary testthat file; the roxytest filename prefix preserves compatibility with existing test and snapshot paths.

# Generated by biocroxytest: do not edit by hand!

# File R/"align-samples.R": @tests

test_that("Function align_samples() @ L13", {
  x <- example_experiment()
  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"))
})

Edit the declaration in R/align-samples.R and regenerate when changing the test. Commit the generated file alongside the source.

Run it and read the result

result <- testthat::test_local(pkg, reporter = "silent")
as.data.frame(result)[c("failed", "error", "passed")]
#>   failed error passed
#> 1      0 FALSE      3

There should be three passing expectations, zero failures and no error. For interactive progress, omit reporter = "silent". In your own package, devtools::test() from its root runs the ordinary suite too.

See the test catch a bug

A tempting implementation uses %in% to select matching columns. It finds the correct samples but retains their original order, returning s1, s3. Change the function’s final line to the following. This R block performs the edit for you:

source_file <- file.path(pkg, "R", "align-samples.R")
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)

The assertions have not changed, so their generated file is still current. Run them again, keeping the result so we can inspect the expected failure:

result <- testthat::test_local(pkg, reporter = "silent", stop_on_failure = FALSE)
as.data.frame(result)[c("failed", "error", "passed")]
#>   failed error passed
#> 1      3 FALSE      0

All three expectations now fail: the order is reversed, along with the counts and conditions. stop_on_failure = FALSE is useful for this deliberate failure; normally keep the default so test failures stop the check.

Restore the correct function and require a passing run:

writeLines(correct, source_file)
result <- testthat::test_local(pkg, reporter = "silent")
as.data.frame(result)[c("failed", "error", "passed")]
#>   failed error passed
#> 1      0 FALSE      3

Apply this to your own package

From your package root, set up ordinary tests with usethis::use_testthat() (install usethis first if needed). Add the roclet above to the existing Roxygen configuration, retaining any other settings. Add @tests beside a function, regenerate, and run devtools::test().

Declare test dependencies in Suggests. Install biocroxytest wherever you regenerate tests; the generated ordinary tests do not need it at runtime. For package checks, build the source and pass the resulting tarball to the check command, for example in a terminal:

R CMD build samplealignment
R CMD check --no-manual samplealignment_0.0.1.tar.gz

Here samplealignment is a persistent copy of the example package in your terminal’s working directory. Substitute your own directory and package version.

Next: share the same checks between memory and HDF5. The next tutorial starts with a fresh copy, so you can also follow it independently.