Tests

Builder automatically generates testthat test files from inline #> test blocks in your R source code. This allows you to keep tests close to the code they’re testing while maintaining a clean separation in the output.

How It Works

When Builder processes your source files, it:

Syntax

Test blocks are defined using #> test and #> endtest directives:

#> test Description of what you're testing
expect_equal(my_function(), expected_value)
expect_true(some_condition)
#> endtest

Important notes:

File Naming Convention

Generated test files follow this pattern:

tests/testthat/test-builder-<filename>

Examples:

Subdirectory paths are flattened using hyphens to keep all test files in a single directory.

Example

Input file (srcr/main.R):

foo <- function() {
  return(1)
}

#> test That it returns one!
expect_equal(foo(), 1)
expect_type(foo(), "double")
#> endtest

Command:

./builder -input srcr -output R

Generated test file (tests/testthat/test-builder-main.R):

test_that("That it returns one!", {
  expect_equal(foo(), 1)
  expect_type(foo(), "double")
})

Multiple Tests

You can have multiple test blocks in a single source file:

add <- function(a, b) {
  return(a + b)
}

#> test Addition works correctly
expect_equal(add(2, 3), 5)
expect_equal(add(-1, 1), 0)
#> endtest

multiply <- function(a, b) {
  return(a * b)
}

#> test Multiplication works correctly
expect_equal(multiply(2, 3), 6)
expect_equal(multiply(0, 5), 0)
#> endtest

All tests from the same source file are collected into a single test file with multiple test_that() blocks.

Cleaning Test Files

When Builder cleans the output directory (default behavior), it also automatically removes the corresponding test files. This ensures a fresh build each time.

# Default: cleans R/ output and tests/testthat/ test files
./builder -input srcr -output R

# Skip cleaning with -noclean flag
./builder -input srcr -output R -noclean

Integration with testthat

The generated test files are standard testthat format and can be run using:

# In R console
library(testthat)
test_dir("tests/testthat")

Or with the standard R CMD check workflow for package development.

Best Practices

Limitations