Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: use_test_helper() #1822

Closed
maelle opened this issue Apr 13, 2023 · 6 comments · Fixed by #1978
Closed

FR: use_test_helper() #1822

maelle opened this issue Apr 13, 2023 · 6 comments · Fixed by #1978
Labels
code 🖥️ R/, tests/ tidy-dev-day 🤓 Tidyverse Developer Day

Comments

@maelle
Copy link
Contributor

maelle commented Apr 13, 2023

I really enjoy using use_test() and use_r(), but was wondering whether there could be an equivalent for creating a tests/testthat/helper- file? (I didn't find any related issue but might have missed one 😅)

@jennybc jennybc added feature a feature request or enhancement code 🖥️ R/, tests/ and removed feature a feature request or enhancement labels Apr 21, 2023
@olivroy
Copy link
Contributor

olivroy commented Oct 20, 2023

I agree. I think it may be a little bit less straightfoward than the other 2 (use_r() and use_test())

Should there be an option for a generic test helper "tests/testthat/helper.R", vs file specific test helper?

Also, important to note that R/helper.R and tests/testthat/helper.R may not be related.

@olivroy
Copy link
Contributor

olivroy commented Mar 19, 2024

I made a little sketch. I don't really like that the default isn't consistent with the default of use_test(), but also I think that having a helper file for each test file by default seems a bit overkill.

usethis/R/r.R

Lines 57 to 71 in fdb4a72

#' @rdname use_r
#' @export
use_test <- function(name = NULL, open = rlang::is_interactive()) {
if (!uses_testthat()) {
use_testthat_impl()
}
path <- path("tests", "testthat", paste0("test-", compute_name(name)))
if (!file_exists(path)) {
use_template("test-example-2.1.R", save_as = path)
}
edit_file(proj_path(path), open = open)
invisible(TRUE)
}

#' @rdname use_r
#' @export
#' @param scope if global, creates "tests/testthat/helper.R", otherwise creates
#'   "tests/testthat/helper-{name}.R
use_test_helper <- function(name = NULL, scope = c("global", "file"), open = rlang::is_interactive()) {
  if (!uses_testthat()) {
    use_testthat_impl()
  }
  scope <- arg_match(scope)

  if (scope == "global") {
    path <- path("tests", "testthat", "helper", ext = "R")
  } else {
    path <- path("tests", "testthat", paste0("helper-", compute_name(name)))
  }

  edit_file(proj_path(path), open = open)
  ui_bullets(c(
    "_" = "Run {.run devtools::load_all()} to load objects from helper files in
         your environment."
  )
  )
  invisible(TRUE)
}

@jennybc
Copy link
Member

jennybc commented Mar 22, 2024

I'm generally on board, so am happy to see this in PR form. I'm not sure the name and scope arguments are quite right yet. I don't have great vocabulary for stuff yet, but clearly we want to make it easy to ask for helper.R versus helper-stuff.R. I think 1 argument is enough. "Suffix" almost captures it, but it's not perfect 🤔

@jennybc
Copy link
Member

jennybc commented Jun 27, 2024

Starting to actually think about this now, including the PRs #1978 and #1990.

Some high-level thoughts (informed by a quick consult with @hadley too):

  • Test helper files don't have the same tight coupling as R/whatever.R and tests/testthat/test-whatever.R. We think of a test helper file as belonging to the package not to a specific R or test file. Which is why the default is a plain old tests/testthat/helper.R.
  • So I definitely don't see the need to support the file creation/opening functionality that use_r()/use_test() provide for managing the pair of files.
  • In terms of what that means for use_test_helper(), that just leaves the initial creation of a helper file. Which might not really justify adding a new function 🤔

@olivroy
Copy link
Contributor

olivroy commented Jun 27, 2024

it would also be useful to access it to add new functions to it.

I would understand that it may not be worth a new function, but typing edit_file("tests/testthat/helper.R") is a bit clumsy.

In terms of promoting good workflow, it would be a + too.,

I find test helpers are life savers for efficient testing workflows (as they are loaded with load_all(), but they are not used that much in the wild.

But otherwise, feel free to close and I will just add this to my .Rprofile/

use_test_helper <- function() usethis::edit_file("tests/testthat/helper.R")

Looking at it, it's true that it doesn't seem worth the effort. Maybe adding this example to the documentation of edit_file() would be a good idea?

@jennybc
Copy link
Member

jennybc commented Jul 22, 2024

I'd be happy to review a small PR with a small function that helps to create (and open) a test helper file, in the right place, with a good name (defaulting to "tests/testthat/helper.R"). The only added feature could perhaps be a way to create "tests/testthat/helper-foo.R", if it's the kind of package with multiple helper files. Let's leave this one available for tidyverse dev day. Ah, I see there is already a PR in flight. Will comment on that, then.

@jennybc jennybc added the tidy-dev-day 🤓 Tidyverse Developer Day label Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code 🖥️ R/, tests/ tidy-dev-day 🤓 Tidyverse Developer Day
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants