Utils and 'RStudio' Addins to Make Testing Even More Fun

Utility functions and 'RStudio' addins for writing, running and organizing automated tests. Integrates tightly with the packages 'testthat', 'devtools' and 'usethis'. Hotkeys can be assigned to the 'RStudio' addins for running tests in a single file or to switch between a source file and the associated test file. In addition, testthis provides function to manage and run tests in subdirectories of the test/testthat directory.

CRAN status


Testhis provides RStudio addins for common package development tasks:

  • test_this(): Run tests associated with the currently open R script file.
  • lest_this(): "Load and test"; As above, but call devtools::load_all() first
  • test_with_skip(): Like devtools::test(), but does not run test files that contain the line #' @skip.
  • open_testfile(): Opens the associated testfile in an editor window. If the currently open file already is a testfile, it opens the associated file in the /R directory. Can be used to jump back and forth between both.

It furhter provides functions for managing subdirectories of the tests/testthat directory of a package:

  • use_testdata() places a single R object in the tests/testhat/testdata directory. It is analoguous to usethis::use_data(), except that it saves the Object in the .rds format, which is more convenient for single R Objects than .rda or .Rdata (see ?readRDS).
  • use_testdata_raw() creates the directory tests/testhat/testdata-raw. Use this directory to put scripts that generate the data in tests/testhat/testdata.
  • read_testdata() is a simple wrapper for readRDS() to read files in tests/testhat/testdata.
  • use_test_subdir() and test_subdir() for putting/running tests in subdirectories of tests/testhat/. These tests will not be run on CRAN or by devtools::test().

Testthis also provides the experimental feature get_test_coverage(). In contrast to the popular covr package, this does not calculate the coverage automatically. It rather requires you to put special comment tags in your test-source files. You can use it like a todo/checklist.



# You can also install the development version from GitHub:
# install.packages("devtools")



open_testfile()  # can be assigned to a hotkey
## The file `tests/testthat/test_README.Rmd` does not exist. 
## You can create it with testthis::test_skeleton().

## * Creating `tests/testthat/test_myscript.R`

## > Opens `tests/testthat/test_myscript.R` (in RStudio)

test_this()  # can be assigned to a hotkey
## > Runs tests in `tests/testthat/test_myscript.R` (in RStudio,)
## > works from the original .R file as well as from the file containing the tests

For a detailed overview of all testthis features with usage examples please refer to the package vignette.


testthis 1.0.4

  • Import parse_ns_file() from pkgload instead of devtools
  • #* @testfile tag paths can now contain the script file extension .R
  • removed deprecated lest_this() function
  • various small bugfixes and improvements

testthis 1.0.3

  • Maintenance release to make vignette comply with new CRAN guidelines
  • test (test_this(), test_subdir(), etc..) functions now save all files in Rstudio and reload the package before running tests.
  • lest_this() has been deprecated. test_this() now behaves like lest_this()

testthis 1.0.2

  • added test_all() to run tests in all subdirectories
  • Use usethis::proj_get() instead of the base_path function argument for compatbility with usethis.

testthis 1.0.1

  • Reorganized package documentation (README, vignette, ?testthis)
  • all pkg = function arguments have been changed to base_path = for consistency with the usethis package.
  • get_test_coverage() now recognizes tests in test subdirs

testthis 1.0.0

  • Reworked an expanded infrastructure functions, such as use_testdata() and use_test_subdir(). See readme for details.
  • test_skeleton() now honours the #* @testfile tag
  • open_test() can now jump back and forth between source and test file, and does no longer automatically create test files.
  • testthis now depends on the new usethis package that replaces some of the functionality from devtools
  • Prevent creation of .rd files for unexported functions with @noRd

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


1.1.1 by Stefan Fleck, a year ago


Report a bug at https://github.com/s-fleck/testthis/issues

Browse source code at https://github.com/cran/testthis

Authors: Stefan Fleck [aut, cre]

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports assertthat, devtools, fs, magrittr, pkgload, rprojroot, stats, stringi, testthat, tools, usethis, utils

Suggests covr, crayon, knitr, rmarkdown, roxygen2, rstudioapi

Suggested by DNAtools, miniCRAN.

See at CRAN