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.
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.
parse_ns_file()from pkgload instead of devtools
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.