Forum:Unit test / integration test packages for R for bioinformatic projects?
1
0
Entering edit mode
12 weeks ago
steve ★ 2.9k

I have a bunch of R code that I need to start re-using across several projects. I do not currently have any sort of integration tests or unit tests established for it. I am used to using unittest in Python, since its built into the standard library so its easy to just git clone my repo and run tests out of the box. However I am having trouble finding the same in R. Seems like most of the unit test libraries are 3rd party thus need extra installation and environment management, etc.. Also from reading code samples for each, I am not clear which are the easiest to use.

What kind of experiences have others had with these libraries? I was leaning towards RUnit (https://cran.r-project.org/web/packages/RUnit/vignettes/RUnit.pdf) but would like to hear what has worked/not worked for you all.

R Forum • 176 views
0
Entering edit mode

and of course, I stumbled upon this page 5 minutes after making this post; https://bioconductor.org/developers/how-to/unitTesting-guidelines/ provides a comparison of RUnit vs testthat

while that article seems a little outdated now, it does appear that RUnit has less frequent development than testthat;

https://cran.r-project.org/web/packages/RUnit/ <- last published 2018

https://cran.r-project.org/web/packages/testthat/index.html <- last published 2021

1
Entering edit mode
12 weeks ago

I've had great luck with testthat, although I've only used it in the context of package development. It's integrated into the whole devtools/usethis paradigm which is the de facto standard, so the package is well maintained and supported.

0
Entering edit mode

I have looked at that one a couple times in the past but never really got past the part where all the docs I saw were oriented around writing packages. I do not want an R package, I just want tests for arbitrary R files. I will look again to see if they have any clear examples of how to do that.

0
Entering edit mode

yeah now I remember, every time I Google for testthat I end up back at this page; https://testthat.r-lib.org/

Usage

The easiest way to get started is with usethis. Assuming you’re in a package directory, just run usethis::use_test("name") to create a test file, and set up all the other infrastructure you need.

When I am looking up how to use a specific new library, the last thing I want to hear is "first you need to go use this other library", and I am never working in a package directory, which means I will have to try and reverse-engineer all the subsequent instructions to try to figure out how to make it work..

1
Entering edit mode

You only need devtools/usethis if you are developing a package. To use testthat standalone is really simple.

library("testthat")

test_that("value check", {
expect_equal(5, expected=5)
expect_equal(5, expected=6)
})

── Failure (Line 3): value check ────────────────────────────────────────────
5 not equal to 6.
1/1 mismatches
[1] 5 - 6 == -1


You can put a bunch of test files into a directory and then run testthat::test_dir() and it will run all the checks from that directory.