Skip to content

Commit

Permalink
Merge pull request #2 from rstudio/test-5-apps
Browse files Browse the repository at this point in the history
testthat/integration tests for 5 apps
  • Loading branch information
alandipert authored Jan 22, 2020
2 parents b3acbcc + 3da69c8 commit 3d8a298
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 17 deletions.
19 changes: 7 additions & 12 deletions 001-hello/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,15 @@ ui <- fluidPage(
# Define server logic required to draw a histogram ----
server <- function(input, output) {

# Histogram of the Old Faithful Geyser Data ----
# with requested number of bins
# This expression that generates a histogram is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$distPlot <- renderPlot({
x <- faithful$waiting

bins <- reactive({
seq(min(x), max(x), length.out = input$bins + 1)
})

x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
output$distPlot <- renderPlot({

hist(x, breaks = bins, col = "#75AADB", border = "white",
hist(x, breaks = bins(), col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")

Expand Down
4 changes: 4 additions & 0 deletions 001-hello/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(shiny)

testthat::test_file("testthat/tests.R")
9 changes: 9 additions & 0 deletions 001-hello/tests/testthat/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library(testthat)
library(shiny)

test_that("reactives update", {
testServer({
session$setInputs(bins = 5)
expect_equal(bins(), seq(43, 96, length.out = 6))
})
})
4 changes: 4 additions & 0 deletions 005-sliders/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(shiny)

testthat::test_file("testthat/tests.R")
26 changes: 26 additions & 0 deletions 005-sliders/tests/testthat/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
library(testthat)
library(shiny)

test_that("inputs are stored", {
testServer({
session$setInputs(
integer = 593,
decimal = 0.6,
range = c(100,300),
format = 5000,
animation = 100
)

# The data frame should have these properties
df <- sliderValues()
expect_equal(nrow(df), 5)
expect_equal(ncol(df), 2)
expect_is(df$Value, "character")

# Updating an input should result in a new plot
plot1 <- output$values
session$setInputs(integer = 594)
plot2 <- output$values
expect_true(plot1 != plot2)
})
})
4 changes: 4 additions & 0 deletions 050-kmeans-example/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(shiny)

testthat::test_file("testthat/tests.R")
16 changes: 16 additions & 0 deletions 050-kmeans-example/tests/testthat/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
library(testthat)
library(shiny)

testServer({
test_that("it works", {
session$setInputs(
xcol = "Sepal.Length",
ycol = "Petal.Length",
clusters = 4
)
expect_equal(colnames(selectedData()), c("Sepal.Length", "Petal.Length"))
expect_equal(nrow(clusters()$centers), 4)
session$setInputs(clusters = 3)
expect_equal(nrow(clusters()$centers), 3)
})
})
8 changes: 3 additions & 5 deletions 119-namespaced-conditionalpanel-demo/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ myPlotUI <- function(id, label = "My Plot") {
}

myPlot <- function(input, output, session, deviates) {
output$scatterPlot <- renderPlot({
x <- rnorm(input$n)
y <- rnorm(input$n)
plot(x, y)
})
x <- reactive(rnorm(input$n))
y <- reactive(rnorm(input$n))
output$scatterPlot <- renderPlot(plot(x(), y()))
}

server <- function(input, output) {
Expand Down
4 changes: 4 additions & 0 deletions 119-namespaced-conditionalpanel-demo/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(shiny)

testthat::test_file("testthat/tests.R")
15 changes: 15 additions & 0 deletions 119-namespaced-conditionalpanel-demo/tests/testthat/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library(testthat)
library(shiny)
library(withr)

# TODO Determine the ideal way for module-reliant test code to load an app
with_dir(shiny:::findApp(), {
local({
source("app.R")
test_that("module works", {
testModule(myPlot, {
expect_true(TRUE)
})
})
})
})
8 changes: 8 additions & 0 deletions 168-supporting-r-dir/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library(testthat)
library(shiny)

# We explicitly pass environment() here because shiny::runTests() has sourced
# the files in R/, and the module we're testing was defined in those files. If
# we don't specify env, testthat uses an environment that doesn't include the
# module, and the tests fail.
testthat::test_file("testthat/tests.R", env = environment())
13 changes: 13 additions & 0 deletions 168-supporting-r-dir/tests/testthat/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library(testthat)
library(shiny)

test_that("counter works", {
testModule(counter, {
inc <- function(x) if (is.null(x)) 0 else x+1
expect_equal(count(), 0)
session$setInputs(button = inc(input$button))
expect_equal(count(), 1)
expect_equal(output$out, "1")
expect_equal(session$returned(), 1)
})
})

0 comments on commit 3d8a298

Please sign in to comment.