Skip to content

Documentation: is package installation necessary (golem/leprechaun) #298

Closed

Description

@russHyde

The documentation for using shinytest2 with a packaged shiny app states that "shinytest2 requires that your package to be installed when testing".
Could you confirm whether or not this is still the case, please.

If I set up a leprechaun app

usethis::create_package("tempGreeter")
leprechaun::scaffold()
devtools::document()

with UI/server:

ui = function(req) {
  fluidPage(
    textOutput("greeting")
  )
}

server = function(input, output, session) {
  output$greeting <- renderText({
    paste0("Hello, World!")
  })
}

Then add shinytest2 infrastructure

shinytest2::use_shinytest2()

Then add a simple test:

# ./tests/testthat/test-e2e-greeting.R
test_that("it displays 'Hello World!'", {
  shiny_app = tempGreeter::run()
  app = shinytest2::AppDriver$new(shiny_app, name = "greeter")

  app$expect_values(screenshot_args = FALSE)
})

A) Without installing the package, the test runs and passes using either devtools::test() at the console or in the RStudio Build window
B) On installing the package (devtools::install()), the test runs and passes as well (either using the R console or the RStudio build window)
C) On changing the package so that output$greeting renders "Hello, Earth!", but without installing the new version of the package, the test fails because of a mismatch between the "Hello Earth!" in the uninstalled package's source code, and the expected "Hello World!" stored in the snapshots.

Is there a subtlety that I've missed? There didn't seem to be a setting where the installed version of the package was used preferentially over the working version when running tests.

Activity

  1. schloerke commented on Jan 5, 2023

    @schloerke
    Collaborator

    OOooooo. Nice work around!

    This approach is different than what I was originally thinking of. Original thought was to use a directory to test. Ex: shinytest2::AppDriver$new(name = "using_app_folder") or shinytest2::AppDriver$new(system.file("apps/ex_app", package = "FOO"), name = "using_app_folder").

    This approach will go to the folder, starts an new R session and try to load everything from scratch. Because there is no hook for "load new code here", it fails to retrieve local updates.

    However, your approach loads the app into the main R session and then {shinytest2} saves the app to disk before testing. This allows for you to load your current app's code into main R session before testing. 🎉


    This approach is clean. Even if you had a FOO:::my_method() call within your server function, it should try to load the locally loaded method from your local FOO package.

    I believe there could be some subtle issues that can come out of making an app object directly, then resaving it somewhere else before testing it. There also might be some serialization issues when sending the app object to the background process. However, the gain of being able to test your local package without having to install would be magnitudes better.

    Ideally, I'm hoping we can just use the {testthat} infrastructure to load the local package instead of {shinytest2}'s save app to disk functionality. Then everything is consistent with how {testthat} executes tests.

  2. russHyde commented on Jan 5, 2023

    @russHyde
    ContributorAuthor

    Thanks, I'm tidying up a couple of blogposts for publication this month and was a bit worried about this. The code above works fine for me, but it says you should install your package on the shinytest2 website. Basically, I didn't want to show a non-recommended way of working with shinytest2 if it might trip them up further down the line.

  3. schloerke commented on Jan 5, 2023

    @schloerke
    Collaborator

    Thank you!

    Thanks, I'm tidying up a couple of blogposts for publication this month

    When your blogs are posted, can you please comment in #299.

  4. added
    theme: testthat file loading
    theme: package / golem / rhino
    on Jan 12, 2023
  5. added this to the v0.4.0 milestone on Jan 18, 2023
  6. schloerke commented on Nov 18, 2025

    @schloerke
    Collaborator

    Closing as #402 attempt to load the local package and adjusted documentation to state installation is no longer required.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    theme: package / golem / rhinotheme: testthat file loading

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions