Unit testing of a deprecated function

I am about to deprecate a function from a CRAN package. This function has a number of unit tests implemented.

The lege artis way of deprecating a function seems to be including .Deprecated("use XYZ instead") call when the function is called.

This seems reasonable, but it triggers a warning in the unit tests - and I am sort of uncomfortable with submitting to CRAN a package known to fail in tests, but on the other hand I do not wish to remove the tests in question altogether (there is a reason I have them in place).

Is there a way out of this conundrum? I am certain I am not the first person facing this...

2 Likes

I also couldn't find a canonical recommendation for testing a deprecated function (e.g. the section of r-pkgs on deprecated functions doesn't mention unit tests), but I agree with you it is a good idea to continue testing until the function is removed entirely.

Here are two ideas:

  1. Suppress the warnings in the unit tests with suppressWarnings(). Depending on how many tests you have, this will require a lot of search and replace. If the tests are spread across multiple files, you could use the RStudio addin Replace in Files. I also did some searching online, and the one post I found also recommended using suppressWarnings().

  2. Have the deprecated function check to see if it is being tested before issuing the deprecation warning. If you are using testthat, you can check for the environment variable TESTTHAT. Thus it could look something like this:

    if (!identical(Sys.getenv("TESTTHAT"), "true"))
      .Deprecated("other_function")
    
3 Likes

Thanks for the pointer!

The guy on SO seems to have had the same reservations as I do - 2 and a half years ahead of me, that is. I should have done my homework better before asking here. Mea culpa.

I will investigate the suppressWarnings() route, as all my tests are in a single continuous block of code, easily wrapped in curly braces. Unit testing of a deprecated function is by definition a temporary issue, so I believe can risk overlooking a legitimate warning.

1 Like

Glad that solution will work for you!

And for the benefit of future readers, could you please mark a solution?

FAQ: How do I mark a solution?

I have investigated the solution and it does work as expected. Thanks!
(I felt the need to make certain by actually running the tests before marking the solution as accepted).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.