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

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.


CRAN status

Overview

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.

Installation

install.packages("testthis")

# You can also install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("s-fleck/testthis")

Usage

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.

News

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.

install.packages("testthis")

1.0.4 by Stefan Fleck, 2 months ago


https://github.com/s-fleck/testthis


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, usethis, testthat, stringi, magrittr, utils, stats, tools, pkgload

Suggests rprojroot, knitr, rmarkdown, rstudioapi, roxygen2


Suggested by miniCRAN.


See at CRAN