Skip to content

FR: use_test_helper() #1822

Closed
Closed

Description

@maelle

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 😅)

Activity

  1. added
    featurea feature request or enhancement
    and removed
    featurea feature request or enhancement
    on Apr 21, 2023
  2. olivroy commented on Oct 20, 2023

    @olivroy
    Contributor

    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.

  3. olivroy commented on Mar 19, 2024

    @olivroy
    Contributor

    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)
    }
  4. jennybc commented on Mar 22, 2024

    @jennybc
    Member

    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 🤔

  5. jennybc commented on Jun 27, 2024

    @jennybc
    Member

    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 🤔
  6. olivroy commented on Jun 27, 2024

    @olivroy
    Contributor

    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?

  7. jennybc commented on Jul 22, 2024

    @jennybc
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions