Refactoring Tests

We all know the saying

When you’ve written the same code 3 times, write a function

However, I would like to expend that to

When you’re written the same test 3 times, write a test function

During my lasting packages such as prismatic, I found myself copy-pasting tests around whenever I needed to test a new function. I realized that the refactoring practices I try to apply in my general code writing, wasn’t carried over to the way I was writing my tests. I would frequency copy-paste hundreds of lines of tests only to replace the function name. In this post will I go over a refactoring scenario I am working on at the moment.

The copy-pasted test

The prismatic package includes almost a dozen different functions that work mostly the same way. They all take the same type of arguments, return the returns in the same fashion and so on. This leads me to have a great overlap between what tests I’m performing for each function.

Taking a look at the following code chuck we see a test that makes sure that the function clr_alpha() complain when given the wrong type of the first argument.

test_that("complain when `col` type is wrong.", {
  expect_error(clr_alpha("not a color"))

  expect_error(clr_alpha(list(pal = "#000000")))

  expect_error(clr_alpha(character()))
})

When looking at the same test for clr_mix() we see that it is a carbon copy except for the function name.

test_that("it complains when col type is wrong.", {
  expect_error(clr_mix("not a color"))

  expect_error(clr_mix(list(pal = "#000000")))

  expect_error(clr_mix(character()))
})

I’m going to propose 2 different styles of refactoring, with the main difference being how RStudio returns the error when tests are not met.

Fix #1 - Plays well with error messages

The first solution is to wrap the inside of your test into a function. The above test would create the refactored testing-function

test_wrong_input <- function(clr_) {
  expect_error(clr_("not a color"))

  expect_error(clr_(list(pal = "#000000")))

  expect_error(clr_(character()))
}

and the test would be changed to

test_that("it complains when col type is wrong.", {
  test_wrong_input(clr_alpha)
})

this change will perform the tests, and adding tests for the new function would only need 1 change in the test instead of 3.

test_that("it complains when col type is wrong.", {
  test_wrong_input(clr_mix)
})

More importantly, let’s imagine that we want to extend the types of wrong inputs we what to screen. Now we simply just need to add it once and it propagates to all the functions.

The main benefit of this refactoring style is that when an error appears, It will denote the line where the test broke.

Fix #2 - Less typing, worse error message

The second solution is to wrap the entire testing statement inside a function. For this example, the function would look like this

test_wrong_input <- function(clr_) {
  test_that("it complains when col type is wrong.", {
  expect_error(clr_("not a color"))

  expect_error(clr_(list(pal = "#000000")))

  expect_error(clr_(character()))
  })
}

and the testing would look like

test_wrong_input(clr_mix)

This reduces the number of lines needed for each test from 3 down to 1. However, it comes with a downside. When an error appears testthat will give the location of the definition of the test-function, not the location from where it was called.

We can still see that the error happens inside the “alpha” Context, but it is slightly harder to track down.

Fix #2.1 - ugly hack to give me the location

The second solution can be made slightly better by making the description of the test more informative.

test_wrong_input <- function(clr_) {
  test_that(paste0("test_wrong_input: ",
                   deparse(substitute(clr_)),
                   "() complains when col type is wrong."), {
  expect_error(clr_("not a color"))

  expect_error(clr_(list(pal = "#000000")))

  expect_error(clr_("pink"))
  })
}

It takes more work upfront when writing the test functions. But it gives a compromise between the brevity of test files and the clarity of the debugging page.

Thanks for reading along! I hope you found it useful!

session information


─ Session info ───────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.0 (2019-04-26)
 os       macOS Mojave 10.14.6        
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2020-04-23                  

─ Packages ───────────────────────────────────────────────────────────────────
 ! package     * version date       lib source        
 P assertthat    0.2.1   2019-03-21 [?] CRAN (R 3.6.0)
 P backports     1.1.6   2020-04-05 [?] CRAN (R 3.6.0)
 P blogdown      0.18    2020-03-04 [?] CRAN (R 3.6.0)
 P bookdown      0.18    2020-03-05 [?] CRAN (R 3.6.0)
 P cli           2.0.2   2020-02-28 [?] CRAN (R 3.6.0)
 P clipr         0.7.0   2019-07-23 [?] CRAN (R 3.6.0)
 P crayon        1.3.4   2017-09-16 [?] CRAN (R 3.6.0)
 P desc          1.2.0   2018-05-01 [?] CRAN (R 3.6.0)
 P details     * 0.2.1   2020-01-12 [?] CRAN (R 3.6.0)
 P digest        0.6.25  2020-02-23 [?] CRAN (R 3.6.0)
 P evaluate      0.14    2019-05-28 [?] CRAN (R 3.6.0)
 P fansi         0.4.1   2020-01-08 [?] CRAN (R 3.6.0)
 P glue          1.4.0   2020-04-03 [?] CRAN (R 3.6.0)
 P htmltools     0.4.0   2019-10-04 [?] CRAN (R 3.6.0)
 P httr          1.4.1   2019-08-05 [?] CRAN (R 3.6.0)
 P knitr       * 1.28    2020-02-06 [?] CRAN (R 3.6.0)
 P magrittr      1.5     2014-11-22 [?] CRAN (R 3.6.0)
 P png           0.1-7   2013-12-03 [?] CRAN (R 3.6.0)
 P R6            2.4.1   2019-11-12 [?] CRAN (R 3.6.0)
 P Rcpp          1.0.4.6 2020-04-09 [?] CRAN (R 3.6.0)
   renv          0.9.3   2020-02-10 [1] CRAN (R 3.6.0)
 P rlang         0.4.5   2020-03-01 [?] CRAN (R 3.6.0)
 P rmarkdown     2.1     2020-01-20 [?] CRAN (R 3.6.0)
 P rprojroot     1.3-2   2018-01-03 [?] CRAN (R 3.6.0)
 P sessioninfo   1.1.1   2018-11-05 [?] CRAN (R 3.6.0)
 P stringi       1.4.6   2020-02-17 [?] CRAN (R 3.6.0)
 P stringr       1.4.0   2019-02-10 [?] CRAN (R 3.6.0)
 P withr         2.1.2   2018-03-15 [?] CRAN (R 3.6.0)
 P xfun          0.13    2020-04-13 [?] CRAN (R 3.6.2)
 P xml2          1.3.0   2020-04-01 [?] CRAN (R 3.6.2)
 P yaml          2.2.1   2020-02-01 [?] CRAN (R 3.6.0)

[1] /Users/emilhvitfeldthansen/Desktop/blogv4/renv/library/R-3.6/x86_64-apple-darwin15.6.0
[2] /private/var/folders/m0/zmxymdmd7ps0q_tbhx0d_26w0000gn/T/RtmpyA4oOr/renv-system-library

 P ── Loaded and on-disk path mismatch.


Emil Hvitfeldt
Emil Hvitfeldt
Research Programmer