# \dontshow{
# Fetch state to restore later
old.stop_on_fail <- getOption("unittest.stop_on_fail")
options(unittest.stop_on_fail = NULL)
old.output <- getOption("unittest.output")
# }
ok(1==1, "1 equals 1")
ok(1==1)
ok(1==2, "1 equals 2")
ok(all.equal(c(1,2),c(1,2)), "compare vectors")
fn <- function () stop("oops")
ok(fn(), "something with a coding error")
ok(c("Some diagnostic", "messages"), "A failure with diagnostic messages")
## Write a failing unit test script
test_path <- tempfile(fileext = ".R")
writeLines('
library(unittest)
ok(1==1)
ok(1==2)
ok(2==2)
ok(3==3)
', con = test_path)
# Without unittest.stop_on_fail, we see all failures:
options(unittest.stop_on_fail = NULL)
tryCatch(source(test_path), error = function (e) { print("=== error ===") })
# With, we stop at the first failing test:
options(unittest.stop_on_fail = TRUE)
tryCatch(source(test_path), error = function (e) { print("=== error ===") })
options(unittest.stop_on_fail = NULL)
## Send unittest output to stderr()
options(unittest.output = stderr())
ok(ut_cmp_equal(4, 5), "4 == 5? Probably not")
## Reset unittest output to default (stdout())
options(unittest.output = NULL)
ok(ut_cmp_equal(4, 5), "4 == 5? Probably not")
# \dontshow{
# Clear unittest result log, so our unittest failues don't fail example-building
unittest:::clear_outcomes()
options(unittest.stop_on_fail = old.stop_on_fail)
options(unittest.output = old.output)
# }
Run the code above in your browser using DataLab