Suppress test_that output "Test passed :)"

Viewed 108

An update to the test_that package (version 3.0.2) in R results in default output for passing a test as:

Test passed :)

This is cute initially, but annoying in a model which performs the test every iteration several thousand times.

How can this message be suppressed?

1 Answers

One option is to extract the message as a string and divert it to an object. We can use capture.output

out <- capture.output(test_that("trigonometric functions match identities", {
 expect_equal(sin(pi / 4), 1 / sqrt(2))
expect_equal(cos(pi / 4), 1 / sqrt(2))
expect_equal(tan(pi / 4), 1)
}))

But functions that suppress the messages would still prints the message

enter image description here


We could make a condition after capturing with grep to print or not

f1 <- function(x) if(any(!grepl('passed', x))) cat(x, sep='\n')

out1 <- capture.output(test_that("trigonometric functions match identities", {

  expect_equal(sin(pi / 4), 1 / sqrt(2))
  expect_equal(cos(pi / 4), 1 / sqrt(2))
  expect_equal(tan(pi / 4), 1)
  expect_equal(sin(pi / 4), 1)

   }))

f1(out) # nothing prints
f1(out1)
#── Failure (???): trigonometric functions match identities ─────────────────────
#sin(pi/4) not equal to 1.
#1/1 mismatches
#[1] 0.707 - 1 == -0.293
Related