Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testthat/integration tests for 5 apps #2

Merged
merged 6 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed given you are passing in env?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know, I think it actually is, because of this bug that I just filed: rstudio/shiny#2746

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out though, I had forgotten why it was necessary.

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)
})
})