Shinytest

Let's say that the Shiny application we have developed runs well on our machine for some input, but for others it does not give the desired output or it gets stuck somewhere and throws errors. In the software development process, testing is one of the most important tasks. A Shiny application might stop working due to any of the following reasons:

  • The version of Shiny and the version of the packages may differ
  • A modification in the application code leads to the wrong input for any other reactive code
  • The data format may have changed

There may be countless reasons that cause us to come across errors and cause the application to stop working. Doing testing manually can be time-consuming and inefficient because you have to consider a wide range of use cases. For this reason, Shiny has the shinytest package for automatic testing. It can be installed as follows (https://www.rdocumentation.org/packages/devtools/versions/1.13.6/topics/install_github):

library(devtools) 
install_github("rstudio/shinytest") 

To carry out a test, follow these steps:

  1. Run recordTest() to launch the app in a test recorder (https://rstudio.github.io/shinytest/reference/recordTest.html):
    library(shinytest) 
 
    # Launch the target app (replace with the correct path) 
    recordTest("path/to/app") 

In the test recorder, we can find a list of recorded events. These events are interactions made by the user. We can also take snapshots of the state of the app by clicking on take snap on the right-hand side window of the recorder.

  1. Quit the test recorder and find the test script in the .R file in the test/ subdirectory. This holds code like the following:
    app<- ShinyDriver$new("..") 
    app$snapshotInit("mytest") 
    app$snapshot() 
    app$setInputs(checkGroup = c("1", "2")) 
    app$setInputs(checkGroup = c("1", "2", "3")) 
    app$setInputs(action = "click") 
    app$snapshot() 

Here, we try the possible use case input and figuring errors. For a more in-depth understanding of the workings of the shinytest package, refer to https://rstudio.github.io/shinytest/articles/shinytest.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset