Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests for deprecated functions in R package cause warnings during check

I've deprecated several functions in my R package by including a .Deprecated("new_function_name") line at the start of the function. I had full unit test coverage for those deprecated functions. Now those tests produce warnings (because of the deprecation message) and muddy up the results of testthat::test() and devtools::check().

I could just delete the test coverage for deprecated functions, but it seems like as long as users can still call the functions, I should retain test coverage. Is there a way I can keep the tests but avoid the clutter in the result of check()? E.g., tell testthat to count them as passing if the expect_equal() still works, ignoring the deprecation warnings?

like image 693
Sam Firke Avatar asked Mar 22 '17 12:03

Sam Firke


1 Answers

.Deprecated produces a warning. So you can always temporarily store the output and wrap that in a call to expect_warning or suppressWarnings if you don't care to test that it gives a warning.

my_dep_fun <- function(x){
   .Deprecated("my_new_fun")
   return(x+1)
}

Using this

> # This is what I expect you're doing right now
> expect_equal(my_dep_fun(3), 4)
Warning message:
'my_dep_fun' is deprecated.
Use 'my_new_fun' instead.
See help("Deprecated") 
> 
> # If we store and use expect_warning we don't get the warning
> expect_warning(tmp <- my_dep_fun(3))
> expect_equal(tmp, 4)
> # Alternatively...
> suppressWarnings(expect_equal(my_dep_fun(3), 4))
> 
like image 92
Dason Avatar answered Oct 21 '22 11:10

Dason