testit: A Simple Package for Testing R Packages

2026-03-07

Title A Simple Package for Testing R Packages
Version 0.17.1
Description Provides two convenience functions assert() and test_pkg() to facilitate testing R packages.
License MIT + file LICENSE
URL https://github.com/yihui/testit
BugReports https://github.com/yihui/testit/issues
Author Yihui Xie ORCID iD [aut, cre], Tomas Kalibera [ctb], Steven Mortimer [ctb]

R-CMD-check Downloads from the RStudio CRAN mirror Codecov test coverage

This package provides two simple functions:

Snapshot testing is also supported via markdown files in _snapshots/ directories.

1 Why?

Because it is tedious to type these commands repeatedly in tests:

message('checking if these numbers are equal...')
stopifnot(all.equal(1, 1+1e-10), 10*.1 == 1)

message('checking if a non-exported function works...')
stopifnot(is.character(package:::utility_foo(x = 'abcd', y = 1:100)))

With the two simple functions above, we type six letters (assert) instead of sixteen (message + stopifnot), and assert is also a more intuitive function name for testing purposes (you assert a fact followed by evidence):

assert('These numbers are equal', {

  (all.equal(1, 1 + 1e-10))

  (10 * .1 == 1)

})

assert('A non-exported function works', {
  res = utility_foo(x = 'abcd', y = 1:100)
  (is.character(res))
})

assert('T is TRUE and F is FALSE by default, but can be changed', {
  (T == TRUE )
  (F == FALSE)

  T = FALSE
  (T == FALSE)
})

2 Snapshot testing

Snapshot tests use Markdown files that combine R code with expected output. Place .md files named test-*.md in the tests/testit/ directory, and they will be automatically run by test_pkg().

Each Markdown file contains R code blocks followed by expected output blocks:

# Test description

```{r}
1:5
```

```
[1] 1 2 3 4 5
```

The R code blocks are marked with ```{r} and output blocks with ```. When tests run, the R code is executed and output is compared to the expected output block. If a markdown file doesn’t have output blocks initially, they will be added automatically. To update snapshots when output changes, run testit::test_pkg(update = TRUE).

Snapshot files are human-readable Markdown, making them easy to review in version control. Optionally, you can write ordinary text anywhere in the file, e.g., to explain the test or provide additional context. Snapshot testing will only compare the output of R code blocks to the expected output blocks, ignoring any other text.

3 R CMD check

Put the tests under the directory pkg_name/tests/testit/ (where pkg_name is the root directory of your package), and write a test-all.R under pkg_name/tests/:

library(testit)
test_pkg('pkg_name')

That is all for R CMD check. For package development, you can Ctrl/Cmd + Shift + T to run tests.

4 Installation

Stable version on CRAN:

install.packages('testit')

Development version:

install.packages('testit', repos = 'https://yihui.r-universe.dev')

5 More

How about testthat? Well, this package is far less sophisticated than testthat. There is nothing fancy in this package. I do not use testthat by myself because I’m too lazy to learn the new vocabulary (testthat::expect_xxx). For testit, I do not need to think if I should use expect_equal, expect_equivalent, or expect_identical; I just write test conditions in parentheses that are expected to return TRUE. That is the one and only rule to remember.

There is no plan to add new features or reinvent anything in this package. It is an intentionally tiny package with zero dependencies.

Xunzi

Although he did not really mean it, Xunzi said something that happens to apply well to unit testing:

不积跬步,无以至千里;不积小流,无以成江海。

This package is free and open source software, licensed under MIT.

Appendix

To cite the package testit in publications, please use:

Xie Y (2026). testit: A Simple Package for Testing R Packages. R package version 0.17.1, https://github.com/yihui/testit.

@Manual{,
  title = {testit: A Simple Package for Testing R Packages},
  author = {Yihui Xie},
  year = {2026},
  note = {R package version 0.17.1},
  url = {https://github.com/yihui/testit},
}