15

I have tests for a package that check functions that may or may not return warnings, for example:

test_that("test", {
  expect_true(is.na(log(NA)))
  expect_true(is.na(log(-1)))
})

I am not interested in checking weather the warnings appeared. Is there a way how I could tell testthat to ignore the warnings and not display them when running devtools::test()?

I know I could pack each function in expect_warning, or suppressWarnings, but instead I'd like to do something like

test_that("test", {  
  ignoreAllTheWarningsInside({
     expect_true(is.na(log(NA)))
     expect_true(is.na(log(-1)))
  })
})

Unfortunately options(warn = -1) also does not seem to work for this.

Tim
  • 7,075
  • 6
  • 29
  • 58
  • 1
    I used the following workaround: `expect_warning(a <- is.na(log(NA)))` and `expect_true(a); ...` – Christoph Dec 15 '16 at 14:31
  • 4
    @Christoph I usually use `expect_warning(expect_true(is.na(log(-1))))`, but the problem is as above: for some functions there will be warnings and for some will not, so I am *not* expecting warnings for each case. Moreover, I would like not to have to pack each function in `suppressWarnings()`, since it's lots of copy-and-pasting. – Tim Dec 15 '16 at 14:36
  • I use `a <- ...` if I want to be able to do several tests on `a` (Just as explanation). But I agree, that handling of warnings is sometimes strange. Do you use `RSudio`? Could it be a problem with `RStudio`? To be honest, I never checked that... – Christoph Dec 15 '16 at 14:41
  • `options(warn = -1)` works for me. – Pavel Obraztcov Jul 28 '21 at 05:24
  • Are you sure that using suppressWarnings like `suppressWarnings({expect_true(is.na(log(NA))); expect_true(is.na(log(-1)))})` does not work ? – Karl Forner Oct 14 '21 at 10:28
  • @KarlForner the question was asked almost 5 years ago, It's hard to comment on it right now. – Tim Oct 14 '21 at 10:45
  • Oh I had not realized it was old. It's still in the list of unanswered questions... – Karl Forner Oct 14 '21 at 14:09

1 Answers1

6

Using suppressWarnings() around your script should do the trick. These examples show where you can use the function within your tests. No need for a custom function as all warnings will be silenced.

testthat::test_that("no suppression", {
  testthat::expect_true({
    warning()
    TRUE
  })
})
#> -- Warning (<text>:2:3): no suppression ----------------------------------------
#> 
#> Backtrace:
#>  1. testthat::expect_true(...)
#>  2. testthat::quasi_label(enquo(object), label, arg = "object")
#>  3. rlang::eval_bare(expr, quo_get_env(quo))

testthat::test_that("suppress inside", {
  testthat::expect_true(
    suppressWarnings({
      warning()
      warning()
      TRUE
    })
  )
})
#> Test passed

testthat::test_that("suppress outside", {
  suppressWarnings(
    testthat::expect_true({
      warning()
      warning()
      TRUE
    })
  )
})
#> Test passed

Created on 2021-11-23 by the reprex package (v2.0.1)

Jordan
  • 169
  • 1
  • 6
  • As far as I remember it didn't work when writing the question but does now, so I'm accepting the answer. – Tim Nov 23 '21 at 20:23