Skip to content

Notes on How to Test in R

Caroline Oliver edited this page Jul 26, 2018 · 6 revisions

Information for these notes can be found at the following 2 links, both by Hadley Wickham:

Link 1: https://journal.r-project.org/archive/2011/RJ-2011-002/RJ-2011-002.pdf

Link 2: http://r-pkgs.had.co.nz/tests.html


Set Up

All information for Set Up can be found at Link 2

  • Package Name: ‘testthat’

  • Run the following code in the console to initialize the package

devtools::use_testthat()

  • By running the above code:
  • a tests/testthat directory is created
  • testthat is added in the DESCRIPTION file under ‘Suggests’
  • the file tests/testthat.R is added to the tests folder

Note: the tests/testthat.R file is NOT where the tests are added


Adding Tests

All information for Adding Tests can be found at Link 1 unless specified otherwise

  • tests are added in the testthat folder (within the tests folder)

  • they are .R files that begin with ‘test’ so that they are recognized by the software

The .R test files are arranged in the following hierarchy:

1. Contexts:

  • highest level of the test structure

  • typically a single context line per file though there can be more

  • contexts are defined as “group tests together into blocks that test re-lated functionality”

  • example:

context(“Descriptive Name for Testing File Function”)

2. Tests:

  • secondary level of the test structure

  • there can be multiple groups of tests per file

  • each test groups together expectations and tests the functionality of a SINGLE function

  • example:

test_that(“Description of what is being tested”, {expectation functions})

3. Expectations

  • expectations are used to test whether a packages function produces the correct value given specified parameters/input values

  • there are 11 types of expectation functions: (function descriptions provided by Link 2)

  • expect_true(x)
  • expect_false(x)
  • expect_is(x, y): checks to make sure an object is of a specified class
  • expect_equal(x, y): checks equality within numerical tolerance (uses the all.equal() function)
  • expect_equivalent(x, y): more leniet version of expect_equal()
  • expect_identical(x, y): checks for exact equivalence between given numerical values
  • expect_matches(x, y): checks a character vector verses a regular expression
  • expect_output(x, y): checks printed output of functions
  • expect_message(x, y): checks output messages of functions
  • expect_warning(x, y): checks warning statements
  • expect_error(x, y): checks error statements

What to Test Tips

All information for What to Test Tips can be found at Link 2

  • Test the external functionality/interface of package functions... NOT the internal. This will lead to more standardized testing that will not have to be changed when the function is modified

  • Each function/calculation within a function should have its own test if deemed complicated enough... simple calculations that one is confident will work every time should not need a test of their own

  • Test functions that are "fragile" or "complicated" as these are the ones that are more apt to break

"Always write a test when you discover a bug"


How to Run Tests

All information for How to Run Tests can be found at Link 1

Make sure to call the testthat library before running any of the following code: library(testthat)

Using source() and test_file()

To test a source file with a single test file use the following code structure:

source("pathToSourceFile/name_of_source_file.R")

Example: source("R/mobr.R")

test_file("pathToTestFile/name_of_test_file.R")

Example: test_file("tests/testthat/test_sphere_dist.R")

By running the above two lines, the functions within the source file that are tested within the test file are checked to see if they pass all tests.

Note: This only uses the one test file specified to test the source code. This means that not all functions within the source code will be checked if one has used multiple test files to cover all functionality of the source code.

This way of testing would be useful if only one function was altered in the source code/package and a single test file cover the altered function.

Using autotest()

  • Autotest re-runs ALL testing when either the package/source code OR a testing file is changed in any way

Autotest has 2 arguments & is run using the following code:

autotest(code_path, test_path)

  • code_path = path to source code

  • test_path = path to all testing code

  • When the above code is run successfully a continuous scan is performed on both directories to look for changes

  • Test file is modified - only that test file will be checked against the source code

  • Code file is modified - all code is reloaded and all tests are performed against the new code

To Quit Autotest:

  • Windows: Ctrl + Break

  • Mac: Escape

  • Command Line: Ctrl + C

Base Sample of Testthat File Structure

context("Some basic sample testing")

test_that("correct values are given for __blank__", {

expect_equal(fromPackage, correctAnswer)

})