Reputation: 10606
I have a test for which if the prerequisites are not met (e.g., missing file or something) I would like to make it fail.
Just for clarification, here's an example I'd like to do:
test_that("...", {
if ( ... precondition to execute the test is not met... ) {
expect_true(FALSE) # Make it fail without going further
}
expect_that( ... real test here ...)
})
Now my question is: Is there any fail()
-like expectation in the testthat package or I have to write expect_true(FALSE)
all the time?
Upvotes: 0
Views: 447
Reputation: 121177
There isn't a fail
function in testthat
at the moment. I think you want something like
fail <- function(message = "Failure has been forced.", info = NULL, label = NULL)
{
expect_that(
NULL,
function(message)
{
expectation(FALSE, message)
},
info,
label
)
}
Usage is, for example,
test_that("!!!", fail())
Upvotes: 4
Reputation: 94317
Failure is not an option...
Try using stop
:
test_that("testingsomething", {
if(file.exists("foo.txt")){
stop("foo.txt already exists")
}
foo = writeFreshVersion("foo.txt")
expect_true(file.exists("foo.txt"))
}
)
Upvotes: 0