@@ -149,6 +149,48 @@ on your shiny application object directly. Unfortunately, the test file
149149will not be able to be saved. Instead, the test commands can be copied
150150into a test script manually.
151151
152+ ## Running Applications with a function
153+
154+ Some packages, such as [ golem] ( https://thinkr-open.github.io/golem/ ) ,
155+ provide a function that runs or returns a shiny application. In this
156+ case, you can create a wrapper function to perform two actions: load the
157+ package and return an app. For example, if your package is named ` mypkg `
158+ and the function that runs the app is ` run_test_app() ` , you can create a
159+ wrapper function like this:
160+
161+ ``` r
162+ run_mypkg_app <- function () {
163+ library(mypkg )
164+ run_test_app()
165+ }
166+
167+ app <- AppDriver $ new(run_mypkg_app , name = " mypkg-app" )
168+ ```
169+
170+ For shorthand, you can also use the package function directly:
171+
172+ ``` r
173+ app <- AppDriver $ new(run_test_app , name = " mypkg-app" )
174+ ```
175+
176+ When using a function to initialize the ` AppDriver ` ,
177+ [ shinytest2] ( https://rstudio.github.io/shinytest2/ ) will automatically
178+ call
179+ [ ` pkgload::load_all() ` ] ( https://pkgload.r-lib.org/reference/load_all.html )
180+ when you execute ` library(<pkg>) ` or ` require(<pkg>) ` to load your dev
181+ package, ` <pkg> ` , in the background R process. Similar to
182+ [ mirai] ( https://mirai.r-lib.org ) , there is no carryover to the execution
183+ within the background R process. All packages may need to be
184+ [ ` library() ` ] ( https://rdrr.io/r/base/library.html ) ’ed again.
185+
186+ [ golem] ( https://thinkr-open.github.io/golem/ ) provides a template
187+ ` run_app() ` function. [ golem] ( https://thinkr-open.github.io/golem/ ) app
188+ authors can use the shorthand:
189+
190+ ``` r
191+ app <- AppDriver $ new(run_app )
192+ ```
193+
152194## Other setup steps
153195
154196There are a few steps that are needed for both types of tests.
@@ -164,24 +206,27 @@ to the `Suggests` section in your `DESCRIPTION` file.
164206 shinytest2
165207
166208When all of these items are in place, you can test your package using
167- ` devtools::install(); testthat::test_local() ` or by running
168- ` R CMD check ` on your package. If you are using the RStudio IDE, you can
169- also run Build -\> Test Package or Build -\> Check Package.
209+ [ ` testthat::test_local() ` ] ( https://testthat.r-lib.org/reference/test_package.html )
210+ or by running ` R CMD check ` on your package. If you are using the
211+ RStudio IDE, you can also run Build -\> Test Package or Build -\> Check
212+ Package.
170213
171- [ shinytest2] ( https://rstudio.github.io/shinytest2/ ) requires that your
172- package to be * installed* when testing.
214+ [ shinytest2] ( https://rstudio.github.io/shinytest2/ ) ** no longer**
215+ requires that your package to be * installed* when testing. It will load
216+ from your local path.
173217[ ` testthat::test_local() ` ] ( https://testthat.r-lib.org/reference/test_package.html )
174218(and related wrappers) eventually call
175219[ ` pkgload::load_all() ` ] ( https://pkgload.r-lib.org/reference/load_all.html )
176- to temporarily source the local R package. You can use
177- [ ` test_local() ` ] ( https://testthat.r-lib.org/reference/test_package.html )
178- to test non-[ shinytest2] ( https://rstudio.github.io/shinytest2/ ) tests,
179- but you will need to install your R package to safely execute your
180- [ shinytest2] ( https://rstudio.github.io/shinytest2/ ) tests. If not
181- installed, it will create a confusing situation where your
182- [ shinytest2] ( https://rstudio.github.io/shinytest2/ ) tests are running on
183- a * different* version of your R package (whichever was last installed),
184- than the rest of your tests (the current source).
220+ to temporarily source the local R package in the foreground for unit
221+ testing. [ shinytest2] ( https://rstudio.github.io/shinytest2/ ) has made
222+ the stance that App authors ** must** call ` library(<pkg>) ` or
223+ ` require(<pkg>) ` on their package within their App. This ensures that
224+ the package is loaded in the background R process that runs the Shiny
225+ application. When ` library*() ` is called,
226+ [ shinytest2] ( https://rstudio.github.io/shinytest2/ ) will automatically
227+ execute
228+ [ ` pkgload::load_all() ` ] ( https://pkgload.r-lib.org/reference/load_all.html )
229+ to load the package’s source code.
185230
186231## How should I test multiple applications?
187232
0 commit comments