Unit testing of a deprecated function

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