test-shiny-app
About
This skill helps developers test Shiny applications using shinytest2 for browser-based end-to-end tests and testServer() for unit testing server logic. It covers snapshot testing, CI integration, and mocking external services. Use it when adding tests to existing apps, setting up testing for new projects, or integrating tests into CI/CD pipelines.
Quick Install
Claude Code
Recommendednpx skills add pjt222/agent-almanac -a claude-code/plugin add https://github.com/pjt222/agent-almanacgit clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/test-shiny-appCopy and paste this command in Claude Code to install this skill
Documentation
測 Shiny 應
立全 Shiny 測——以 shinytest2 為端對端與 testServer() 為單測。
用
- 加測於既存 Shiny 應→用
- 為新 Shiny 案立測策→用
- 重構 Shiny 碼前書回歸測→用
- Shiny 應測整於 CI/CD→用
入
- 必:Shiny 應路
- 必:測範(單測、端對端、皆)
- 可:是否用影測(默:e2e 為是)
- 可:CI 臺(GitHub Actions、GitLab CI)
- 可:所獨測之模
行
一:裝測依
install.packages("shinytest2")
# For golem apps, add as a Suggests dependency
usethis::use_package("shinytest2", type = "Suggests")
# Set up testthat infrastructure if not present
usethis::use_testthat(edition = 3)
得:shinytest2 裝、testthat 目構在。
敗:shinytest2 需 chromote(無頭 Chrome)。系裝 Chrome/Chromium。WSL 上:sudo apt install -y chromium-browser。以 chromote::find_chrome() 驗。
二:書 testServer() 模單測
建 tests/testthat/test-mod_dashboard.R:
test_that("dashboard module filters data correctly", {
testServer(dataFilterServer, args = list(
data = reactive(iris),
columns = c("Species", "Sepal.Length")
), {
# Set inputs
session$setInputs(column = "Species")
session$setInputs(value_select = "setosa")
session$setInputs(apply = 1)
# Check output
result <- filtered()
expect_equal(nrow(result), 50)
expect_true(all(result$Species == "setosa"))
})
})
test_that("dashboard module handles empty data", {
testServer(dataFilterServer, args = list(
data = reactive(iris[0, ]),
columns = c("Species")
), {
# Module should not error on empty data
expect_no_error(session$setInputs(column = "Species"))
})
})
要模:
testServer()測模服邏無瀏- 經
args傳反參 - 用
session$setInputs()擬用交 - 直名訪反返值
- 測邊:空資、NULL 入、無效值
得:模測過於 devtools::test()。
敗:testServer() 誤「not a module server function」→確函內用 moduleServer()。session$setInputs() 不觸反→設後加 session$flushReact()。
三:書 shinytest2 端對端測
建 tests/testthat/test-app-e2e.R:
test_that("app loads and displays initial state", {
# For golem apps
app <- AppDriver$new(
app_dir = system.file(package = "myapp"),
name = "initial-load",
height = 800,
width = 1200
)
on.exit(app$stop(), add = TRUE)
# Wait for app to load
app$wait_for_idle(timeout = 10000)
# Check that key elements exist
app$expect_values()
})
test_that("filter interaction updates the table", {
app <- AppDriver$new(
app_dir = system.file(package = "myapp"),
name = "filter-interaction"
)
on.exit(app$stop(), add = TRUE)
# Interact with the app
app$set_inputs(`filter1-column` = "cyl")
app$wait_for_idle()
app$set_inputs(`filter1-apply` = "click")
app$wait_for_idle()
# Snapshot the output values
app$expect_values(output = "table")
})
要模:
AppDriver$new()啟應於無頭 Chrome- 常用
on.exit(app$stop())清 - 模入 ID 用
"moduleId-inputId"式 app$expect_values()建/比影檔app$wait_for_idle()確反更畢
得:端對端測建影檔於 tests/testthat/_snaps/。
敗:Chrome 不在→設 CHROMOTE_CHROME 環變為 Chrome 二路。影 CI 敗本地過→察臺依繪差——資影用 app$expect_values() 而非視 app$expect_screenshot()。
四:交錄測(可)
shinytest2::record_test("path/to/app")
此於瀏開應含錄面板。交應、後點「Save test」自生測碼。
得:測檔生於 tests/testthat/ 含錄交。
敗:錄不開→先察應於 shiny::runApp() 行成。錄需行應。
五:立影管
影測管期值:
# Accept new/changed snapshots after review
testthat::snapshot_accept("test-app-e2e")
# Review snapshot differences
testthat::snapshot_review("test-app-e2e")
加影目於版控:
tests/testthat/_snaps/ # Committed — contains expected values
得:影檔於 git 為回歸偵。
敗:影不期變→行 testthat::snapshot_review() 見差。納意改以 testthat::snapshot_accept()。
六:整於 CI
加於 .github/workflows/R-CMD-check.yaml 或建專流:
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y chromium-browser
- name: Set Chrome path
run: echo "CHROMOTE_CHROME=$(which chromium-browser)" >> $GITHUB_ENV
- name: Run tests
run: |
Rscript -e 'devtools::test()'
golem 應→確應包先裝乃測:
- name: Install app package
run: Rscript -e 'devtools::install()'
得:測過於 CI 含無頭 Chrome。
敗:常 CI 患:Chrome 未裝(加 apt-get 步)、顯服缺(shinytest2 默用無頭、常非患)、緩行超時(增 AppDriver$new() 之 timeout)。
驗
-
devtools::test()行諸測無誤 - testServer() 測覆模服邏
- shinytest2 測覆要用流
- 影檔入版控
- 測過於 CI 境
- 邊測(空資、NULL 入、誤態)
忌
- 測 UI 繪非邏:用
testServer()為邏、app$expect_values()為資。唯視覺重時用app$expect_screenshot()——影脆於跨臺 - e2e 模 ID 式:經 AppDriver 設模入用
"moduleId-inputId"(連號分)、非"moduleId.inputId" - 時不穩:
app$set_inputs()後常呼app$wait_for_idle()。否則斷或行於反更前 - 影漂:勿入異臺生影(Mac 對 Linux)。標 CI 臺生影
- CI 無 Chrome:shinytest2 需 Chrome/Chromium。常含裝步於 CI 流
參
build-shiny-module— 建可測之明介模scaffold-shiny-app— 立應構含測基write-testthat-tests— R 包通 testthat 模setup-github-actions-ci— R 包(golem 應)CI/CD 立
GitHub Repository
Related Skills
evaluating-llms-harness
TestingThis Claude Skill runs the lm-evaluation-harness to benchmark LLMs across 60+ standardized academic tasks like MMLU and GSM8K. It's designed for developers to compare model quality, track training progress, or report academic results. The tool supports various backends including HuggingFace and vLLM models.
cloudflare-cron-triggers
TestingThis skill provides comprehensive knowledge for implementing Cloudflare Cron Triggers to schedule Workers using cron expressions. It covers setting up periodic tasks, maintenance jobs, and automated workflows while handling common issues like invalid cron expressions and timezone problems. Developers can use it for configuring scheduled handlers, testing cron triggers, and integrating with Workflows and Green Compute.
webapp-testing
TestingThis Claude Skill provides a Playwright-based toolkit for testing local web applications through Python scripts. It enables frontend verification, UI debugging, screenshot capture, and log viewing while managing server lifecycles. Use it for browser automation tasks but run scripts directly rather than reading their source code to avoid context pollution.
finishing-a-development-branch
TestingThis skill helps developers complete finished work by verifying tests pass and then presenting structured integration options. It guides the workflow for merging, creating PRs, or cleaning up branches after implementation is done. Use it when your code is ready and tested to systematically finalize the development process.
