Skip to content

fail function for forcing a test failure #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
richierocks opened this issue Sep 20, 2012 · 2 comments
Closed

fail function for forcing a test failure #47

richierocks opened this issue Sep 20, 2012 · 2 comments

Comments

@richierocks
Copy link

There's been a request for a fail function in testthat, that just makes a test fail.

http://stackoverflow.com/questions/12507229/how-to-make-a-test-fail-with-the-testthat-package

I had a go at writing one, for possible inclusion in the package.

' Makes a testfail

'

' The fail function forces a test to fail.

'

' @param message a string to display.

' @param infoextra information to be included in the message (useful when writing tests in loops).

fail <- function(message = "Failure has been forced.", info = NULL)
{
expect_that(
NULL,
function(message)
{
expectation(FALSE, message)
},
info = info,
NULL
)
}

Usage is, for example,

test_that("failure test", fail())
@richierocks
Copy link
Author

Since the utility of this function isn't very clear, here's a possible use case. It isn't life-changing, but I think it qualifies as a minor perk.

When you want to test a new function, it can be quite useful to write down all the tests that you can think of in one go. Rather than worrying about how each test needs to be implemented, you just want a placeholder that fails, in order to let you know that you haven't written that test yet. After you've decided upon each test, you can go back and replace the fails with real tests.

@spacedman
Copy link

You could just stick a stop("Not done this yet") in your test - that will show as an error rather than a failure in the test runner. Semantically I find the idea of a failure being conjured from thin air quite disturbing, whereas saying 'stop!' seems more natural. HMMV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants