Learn R Programming

testex test examples

Add tests and assertions in-line in examples

Quick Start

Set up your package to use testex using

testex::use_testex()

and then start adding tests!

#' Hello, World!
#' 
#' @examples
#' hello("World")
#' @test "Hello, World!"
#'
#' hello("darkness my old friend")
#' @test grepl("darkness", .)
#' 
#' @export
hello <- function(who) {
  paste0("Hello, ", who, "!")
}

If you were already using testthat, you'll immediately see a new test context for testing your examples. And if you aren't using testthat, then you'll find that your tests are being run with your examples when you run R CMD check

roxygen2 tags

@test

will check that the result of the last example is identical to your test. You can use the example output in a function using a ..

#' @examples
#' sum(1:10)
#' @test 55
#' @test is.numeric(.)

@testthat

is similar, but has the added benefit of automatically inserting a . into testthat::expect_* functions.

#' @examples
#' sum(1:10)
#' @testthat expect_equal(55)
#' @testthat expect_vector(numeric())

Prior Art

  • roxytest A slightly different approach. Allows tests to be written in-line, but generates test files used directly by a testing framework.

Copy Link

Version

Install

install.packages('testex')

Monthly Downloads

577

Version

0.2.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Doug Kelkhoff

Last Published

November 12th, 2025

Functions in testex (0.2.1)

srclocs

Build a source location from a minimal numeric vector
with_srcref

Raise testthat Expectations With A Known Source Reference
wrap_expect_no_error

Wraps an example expression in a testthat expectation to not error
srcref_key

Convert a srcref to a character representation
test_files

Test a list of files
test_examples_as_testthat

Execute examples from Rd files as testthat tests
with_attached

Temporarily attach a namespace
vlapply

vapply shorthand alternatives
testex-options

Cached retrieval of testex options from package DESCRIPTION
testex-rd-example-helpers

Rd Example Parsing Helpers
testex

A syntactic helper for writing quick and easy example tests
use_testex

Add testex tags and configure package to fully use testex features
use_testex_as_testthat

Run examples as testthat expectations
uses_roxygen2

Checks for use of roxygen2
is_r_cmd_check

Test whether currently executing R checks
package-file-helpers

Package source file helpers
get_example_value

Determine which symbol to use by default when testing examples
fallback_expect_no_error

Expect no Error
file_line_nchar

Return the number of characters in a line of a file
testex-roxygen-tags

testex roxygen2 tags
testex-testthat

Support for testthat Expectations
string_newline_count

Get String Line Count
as.srcref

srcref_nlines

deparse_pretty

Deparse pretty
s3_register

Register a method for a suggested dependency
split_srcref

Split a Source Reference at specific lines
deparse_indent

Deparse an expression and indent for pretty-printing