testthis

CRAN status Travis build status Codecov test coverage

testthis makes unit testing in R more comfortable. It is designed to complement the packages testthat, devtools and usethis.

Overview

testthis provides RStudio addins for working with files that contain tests. These can be called like normal R function, but it is also possible to assigned them to hotkeys RStudio (Tools/Modify Keyboard Shortcuts).

The associated test file for a file R/foo.R is usually /tests/testhat/test_foo.R (or test-foo.R). You can modify this behavior by putting the comment #* @testfile anotherfile anywhere in R/foo.R.

testthis also provides functions for managing tests in subdirectories of tests/testthat:

Testthis also provides some simple code analysis tools

For more details see the function reference

Installation

# Testthis is on CRAN:
install.packages("testthis")

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

Usage

library(testthis)

RStudio addins (can be assigned to hotkeys):

open_testfile()
## The file `tests/testthat/test_myscript.R` 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`

test_this() 
## > Runs tests in `tests/testthat/test_myscript.R`
## > works from the original .R file as well as from the file containing the tests

Code analysis:

test_index()
test-index
test_coverage()

## > Package /data/home/fleck/rpkgs/rotor, Test Coverage: 17.8%
## >
## > exported functions .................... 
## > u fun          
## > + backup       
## > + backup_date  
## > + backup_time  
## > ...

# 'u' stands for 'unit tests', more columns are possible if you use the 
# test_subdir() feature of testthis