Question

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?

Was it helpful?

Solution

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())

OTHER TIPS

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"))
}
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top