R/expect-silent.R

Defines functions expect_silent

Documented in expect_silent

#' Do you expect code to execute silently?
#'
#' Checks that the code produces no output, messages, or warnings.
#'
#' @inheritParams expect_error
#' @return The first argument, invisibly.
#' @family expectations
#' @export
#' @examples
#' expect_silent("123")
#'
#' f <- function() {
#'   message("Hi!")
#'   warning("Hey!!")
#'   print("OY!!!")
#' }
#' \dontrun{
#' expect_silent(f())
#' }
expect_silent <- function(object) {
  act <- quasi_capture(enquo(object), NULL, evaluate_promise)

  outputs <- c(
    if (!identical(act$cap$output, "")) "output",
    if (length(act$cap$warnings) > 0) "warnings",
    if (length(act$cap$messages) > 0) "messages"
  )

  if (length(outputs) != 0) {
    fail(c(
      sprintf("Expected %s to run silently.", act$lab),
      sprintf("Actual noise: %s.", paste(outputs, collapse = ", "))
    ))
  } else {
    pass()
  }
  invisible(act$cap$result)
}

Try the testthat package in your browser

Any scripts or data that you put into this service are public.

testthat documentation built on Nov. 25, 2025, 5:09 p.m.