协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
test-shiny-app【开发与工程】Test Shiny applications using shinytest2 for end-to-end browser tests and testServer() for unit-testing module server logic. Covers snapshot testing, CI integration, and mocking external services. Use when adding tests to an existing Shiny application, setting up a testing strategy for a new Shiny project, writing regression tests before refactoring …
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
立 Shiny 應之全試,用 shinytest2(端至端)與 testServer()(單元)。
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() 驗。
立 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() 仿用之交互得:模組試以 devtools::test() 過。
敗則:若 testServer() 誤於「not a module server function」,確函內用 moduleServer()。若 session$setInputs() 不觸反應,於設入後加 session$flushReact()。
立 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() 啟應於無頭 Chromeon.exit(app$stop()) 以清"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() 受意之變。
加於 .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(),資宜 app$expect_values()。視外當要時方用 app$expect_screenshot()——截圖跨臺脆。"moduleId-inputId" 之格(連字符),非 "moduleId.inputId"。app$set_inputs() 後恆呼 app$wait_for_idle()。否則斷或於反應新成前行。build-shiny-module — 立可試之模組與明介scaffold-shiny-app — 立應構與試之基write-testthat-tests — R 包之通 testthat 模setup-github-actions-ci — R 包(golem 應)之 CI/CD 立