Last active
July 3, 2019 20:12
-
-
Save andremueller/d571c2be28ac101e671c2138ad464ece to your computer and use it in GitHub Desktop.
Unit Testing with R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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