Utility functions and 'RStudio' addins to ease the life of people using 'testthat', 'devtools' and 'usethis' in their package development workflow. Hotkeyable addins are provided for such common tasks as switching between a source file and an associated test file, or running unit tests in a single file. 'testthis' also provides utility function to manage and run tests in subdirectories of the test/testthat directory.
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::test(), but does not run test files that contain the line
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
/Rdirectory. 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/testdatadirectory. It is analoguous to
usethis::use_data(), except that it saves the Object in the
.rdsformat, which is more convenient for single R Objects than
use_testdata_raw()creates the directory
tests/testhat/testdata-raw. Use this directory to put scripts that generate the data in
read_testdata()is a simple wrapper for
readRDS()to read files in
test_subdir()for putting/running tests in subdirectories of
tests/testhat/. These tests will not be run on CRAN or by
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.
install.packages("testthis") # You can also install the development version from GitHub: # install.packages("devtools") devtools::install_github("s-fleck/testthis")
library(testthis) 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(). test_skeleton() ## * Creating `tests/testthat/test_myscript.R` open_testfile() ## > 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.
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
test_all()to run tests in all subdirectories
usethis::proj_get()instead of the
base_pathfunction argument for compatbility with usethis.
pkg =function arguments have been changed to
base_path =for consistency with the usethis package.
get_test_coverage()now recognizes tests in test subdirs
use_test_subdir(). See readme for details.
test_skeleton()now honours the
open_test()can now jump back and forth between source and test file, and does no longer automatically create test files.