Skip to content
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

FR: Introduce negative variants for condition expectations? #1642

Closed
IndrajeetPatil opened this issue Jun 8, 2022 · 3 comments
Closed

FR: Introduce negative variants for condition expectations? #1642

IndrajeetPatil opened this issue Jun 8, 2022 · 3 comments

Comments

@IndrajeetPatil
Copy link

The current approach of using NA to test that a condition should not be observed is slightly confusing (at least for me) because it doesn't clearly convey the intent. For example, using expect_error(..., NA) to test that you don't expect an error.

But I am possibly not alone in thinking this way. A cursory search restricted only to GitHub reveals that many developers seem to be using custom negative variants (expect_no_error(), expect_no_warning(), expect_no_message()) to test that the code does not throw errors, warnings, or messages.

So, it will be nice if {testthat} natively supported these functions as an additional way to test for absence of errors, warnings, and messages.

Happy to make a PR if you think this is worth supporting.

#' @export
#' @rdname expect_error
expect_no_error <- function(object) {
  expect_error({{ object }}, NA)
}

#' @export
#' @rdname expect_error
expect_no_warning <- function(object) {
  expect_warning({{ object }}, NA)
}

#' @export
#' @rdname expect_error
expect_no_message <- function(object) {
  expect_message({{ object }}, NA)
}
@IndrajeetPatil
Copy link
Author

cc @MichaelChirico Related to a discussion we had on {lintr}.

@hadley
Copy link
Member

hadley commented Jun 22, 2022

I think using NA as a sentinel for missing, i.e. expect that errors are missing is already adequate and don't currently have plans to add these specific negations.

@hadley hadley closed this as completed Jun 22, 2022
@IndrajeetPatil
Copy link
Author

Related: #1679

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