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

Open
maelle opened this issue Apr 13, 2023 · 3 comments · May be fixed by #1978
Open

FR: use_test_helper() #1822

maelle opened this issue Apr 13, 2023 · 3 comments · May be fixed by #1978
Labels
code 🖥️ R/, tests/

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 🤔

@olivroy olivroy linked a pull request Apr 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code 🖥️ R/, tests/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants