Description Usage Arguments Examples
Offer a way to test the reactive interactions in Shiny — either in Shiny modules or in the server portion of a Shiny application. For more information, visit the Shiny Dev Center article on integration testing.
1 2 3 | testModule(module, expr, ...)
testServer(expr, appDir = NULL)
|
module |
The module to test |
expr |
Test code containing expectations. The test expression will run
in the module's environment, meaning that the module's parameters (e.g.
|
... |
Additional arguments to pass to the module function. These
arguments are processed with |
appDir |
The directory root of the Shiny application. If |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | module <- function(input, output, session, multiplier = 2, prefix = "I am ") {
myreactive <- reactive({
input$x * multiplier
})
output$txt <- renderText({
paste0(prefix, myreactive())
})
}
# Basic Usage
# -----------
testModule(module, {
session$setInputs(x = 1)
# You're also free to use third-party
# testing packages like testthat:
# expect_equal(myreactive(), 2)
stopifnot(myreactive() == 2)
stopifnot(output$txt == "I am 2")
session$setInputs(x = 2)
stopifnot(myreactive() == 4)
stopifnot(output$txt == "I am 4")
# Any additional arguments, below, are passed along to the module.
}, multiplier = 2)
# Advanced Usage
# --------------
multiplier_arg_name = "multiplier"
more_args <- list(prefix = "I am ")
testModule(module, {
session$setInputs(x = 1)
stopifnot(myreactive() == 2)
stopifnot(output$txt == "I am 2")
# !!/:= and !!! from rlang are used below to splice computed arguments
# into the testModule() argument list.
}, !!multiplier_arg_name := 2, !!!more_args)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.