testit v0.12


Monthly downloads



A Simple Package for Testing R Packages

Provides two convenience functions assert() and test_pkg() to facilitate testing R packages.



Build Status Coverage Status Downloads from the RStudio CRAN mirror

This package provides two simple functions (30 lines of code in total):

  • assert(fact, ...): think of it as message(fact) + stopifnot(...)

  • test_pkg(package): runs tests with all objects (exported or non-exported) in the package namespace directly available, so no need to use the triple-colon package:::name for non-exported objects


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)

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

  (T == FALSE)

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/:


That is all for R CMD check. For package development, it is recommended to use devtools. In particular, Ctrl/Cmd + Shift + L in RStudio makes all objects in a package visible to you, and you can run tests interactively.


Stable version on CRAN:


Development version:



How about testthat? Well, this package is far less sophisticated than testthat. There is nothing fancy in this package. Please do consider testthat if your tests require more granularity. I myself do not use testthat 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 only single rule to remember.

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


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 GPL-3.

Functions in testit

Name Description
assert Assertions with an optional message
test_pkg Run the tests of a package in its namespace
has_warning Check if an R expression produces warnings or errors
No Results!

Last month downloads


Type Package
License GPL-3
URL https://github.com/yihui/testit
BugReports https://github.com/yihui/testit/issues
RoxygenNote 7.1.1
Encoding UTF-8
NeedsCompilation no
Packaged 2020-09-24 20:51:17 UTC; yihui
Repository CRAN
Date/Publication 2020-09-25 05:00:02 UTC
suggests rstudioapi
Contributors Steven Mortimer

Include our badge in your README