Site icon R-bloggers

Two recent enhancements to my testing workflow

[This article was first published on Maƫlle's R blog on Maƫlle Salmon's personal website, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I spend a lot of quality time with testthat, that sometimes deigns to praise my code with emojis, sometimes has to encourage me. No one gets it right on the first time apparently?

Anyway, in honor of testthat 3.2.0 release šŸŽ‰ šŸ‘, Iā€™d like to mention two small things that improved my testing workflow a whole lot!

Running one single test at a time

Under testthat 3.2.0 minor features lies a small gem:

test_file() gains a desc argument which allows you to run a single test from a file. #1776

This is huge! When I am struggling with a single test called ā€œMy great function worksā€, I can now call devtools::test_active_file(desc = "My great function works")! Iā€™m not entirely sure yet it perfectly integrates with snapshot recording, but Iā€™m very happy nonetheless.

Now if I were struggling with more times at a time (Remember, no one gets it right on their first time šŸ˜…), I should use the handy lazytest package.

Getting back to the package directory and project

I recently realized that ifā€¦

the wonderful studioapi::getActiveProject() function remembers where the actual project, that is your package, is, very much like clicking on the RStudio hex logo in the Files tab.

Based on that knowledge I added a helper function to the tests of a package I work on, to have it at hand after running devtools::load_all():

back_to_start <- function() {
  current_dev_project <-  rstudioapi::getActiveProject()
  setwd(current_dev_project)
  usethis::proj_set(current_dev_project)
}

So I can make a mess in my console and then run back_to_start() to get back where I started.

Conclusion

In this short post I presented two recent enhancements to my testing workflow: the adoption of the new desc argument of testthat::test_file() to run one single test at a time; and the ā€œdiscoveryā€ of a handy way to find my way back to the package directory after experimenting in temporary toy directories.

To leave a comment for the author, please follow the link and comment on their blog: Maƫlle's R blog on Maƫlle Salmon's personal website.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version