Forum: Unit test / integration test packages for R for bioinformatic projects?
0
gravatar for steve
15 days ago by
steve2.9k
United States
steve2.9k wrote:

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.

forum R • 83 views
ADD COMMENTlink written 15 days ago by steve2.9k

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

ADD REPLYlink modified 15 days ago • written 15 days ago by steve2.9k
1
gravatar for rpolicastro
15 days ago by
rpolicastro3.9k
Bloomington, IN
rpolicastro3.9k wrote:

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.

ADD COMMENTlink written 15 days ago by rpolicastro3.9k

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.

ADD REPLYlink written 15 days ago by steve2.9k

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..

ADD REPLYlink written 15 days ago by steve2.9k
1

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.

ADD REPLYlink modified 15 days ago • written 15 days ago by rpolicastro3.9k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2175 users visited in the last hour
_