README.md

testextra

Travis build status Coverage CRAN status life-cycle

The goal of testextra is to facilitate extraction of tests embedded in source code.

Installation

You will be able to install the released version of testextra from CRAN once it is released with:

install.packages("testextra")

Until then or if you wish to get the latest version prior to release you may install directly from GitHub with:

remotes::install_github("RDocTaskForce/testextra")

Including Tests in Source Code.

To include tests in source code put your tests following the function definition nested in an if(FALSE){...} block and tag it with a #@testing block tag.

#' Hello World Example
hello_world <- function(){
    message("hello world!")
}
if(FALSE){#@testing
    expect_message(hello_world(), "hello world!")
}

Assuming the preceding code is in a package file ./R/hello_world.R running the extract_tests() command will create a file ./tests/testthat/test-hello_world.R with the contents as below.

#! This file was automatically produced by the testextra package.
#! Changes will be overwritten.

context('tests extracted from file `hello_world.R`')
#line 5 "./R/hello_world.R"
test_that('hello_world', {#@testing
    expect_message(hello_world(), "hello world!")
})

When run, if there are error messages, the line given will be the line and file from the original source code.

Combination Functions

The functions test() and test_file() provided in testextra will both extract tests from source files, run said tests, and output the results. test() operates on a package as a whole or a subset of a package by setting the filter argument, see the help file for details. test_file() is intended to work with the RStudio GUI. It takes the currently selected file, extracts tests and runs the tests. This way a tests may be run only for the current file being evaluated.

Both test() and test_file() are available through the add-ins, and made accessible through the menu of RStudio.

Other Helpers

The 'testextra` package provides a number of useful testing functions to use when testing code.

Inheritance

Strings

Validity

Namespaces

When testing dynamic class creation and modification, it is often necessary to have a package environment other that the package environment in which the creation functions are defined. For this purpose, testextra provides these namespace manipulation functions.

Others

A few other helpers that do not fit into one of the above categories.

Documentation

The testextra package is developed by the R Documentation Task Force, an R Consortium Infrastructure Steering Committee working group.



Try the testextra package in your browser

Any scripts or data that you put into this service are public.

testextra documentation built on Dec. 18, 2019, 9:38 a.m.