Skip to content

Instantly share code, notes, and snippets.

@andremueller
Last active July 3, 2019 20:12
Show Gist options
  • Save andremueller/d571c2be28ac101e671c2138ad464ece to your computer and use it in GitHub Desktop.
Save andremueller/d571c2be28ac101e671c2138ad464ece to your computer and use it in GitHub Desktop.
Unit Testing with R
# Example for simple function to be tested
#
# 1. Change working directory
# 2. Call: testthat::test_dir('tests') within RStudio or R
increment <- function(value) {
value + 1
}
source("../my_code.R", chdir = TRUE)
library(testthat)
test_that("single number", {
expect_equal(increment(-1), 0)
expect_equal(increment(0), 1)
})
test_that("vectors", {
expect_equal(increment(c(0,1)), c(1,2))
})
test_that("empty vector", {
expect_equal(increment(c()), c())
})
test_that("test NA", {
expect_true(is.na(increment(NA)))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment