0

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?

rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • If you just expect the precondition tests to pass (expect_true), then the fail will happen naturally... I still don't see why my answer isn't right. – Spacedman Sep 20 '12 at 11:40
  • @Spacedman The reason is that you don't answer my question, which you can find in bold in my questoin :-) Is there a `fail()`-like expectation in *testthat*? – rlegendi Sep 20 '12 at 12:32
  • So what you want is not a 'fail()-like expectation' (which is a bit meaningless) but a way of raising a fail condition? `stop("reason for fail")` might do it. – Spacedman Sep 20 '12 at 13:15
  • Other testing tools like JUnit for instance [supports it](http://junit.sourceforge.net/javadoc/org/junit/Assert.html#fail()). As I demonstrated, `expect_true(FALSE)` is a working solution, but I was interested if there is a built in mechanism (i.e., an *expectation* in the *testthat* library). – rlegendi Sep 20 '12 at 14:01
  • The docs and source code will swiftly tell you there isn't an exact equivalent. I'm not sure it has much value though, since most of the time I reckon it would be better to fail on your requirements rather than forcing it. Whatever. – Spacedman Sep 20 '12 at 16:02
  • `testthat::fail()` – hplieninger Aug 22 '17 at 09:56

2 Answers2

4

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())
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
0

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"))
}
)
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • As I written above, I want to fail a test if the conditions to run it is not met, e.g.: `foo()` writes to a file, and I want to make sure it is non existent before and exists after the function call (and yes, inside a `test_that` block you mentioned). But if the file exists, I might consider failing the test before it cannot be verified if `foo()` creates it. – rlegendi Sep 20 '12 at 07:57
  • Then you need to check your prerequisites in your code block. Stuff that you expect to fail or want to check the return value of goes into `expect_something()` parts, code that you want to work or to return a test fail if it doesn't work just goes into the test_that block. I'll edit a bit. – Spacedman Sep 20 '12 at 08:34
  • Tried to clarify the question. – rlegendi Sep 20 '12 at 11:38