You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在golem框架中使用shinytest进行Shiny应用测试?

在golem框架中配置shinytest并结合testthat测试

1. shinytest脚本的存放路径

golem框架下,shinytest脚本需放在包根目录的tests/testthat/shinytest文件夹中——这个目录需要你手动创建,因为golem默认将UI/Server拆分为包内函数,不会自动生成shinytest的存放目录。

2. 结合testthat与expect_pass的测试实现

步骤1:定义启动golem应用的辅助函数

在测试目录下的helper.R或直接在测试脚本开头,写一个启动应用的函数:

start_golem_app <- function() {
  # 调用golem生成的默认启动函数,替换为你的包名
  yourpackage::run_app()
}

步骤2:编写shinytest交互脚本

在tests/testthat/shinytest下创建测试脚本(比如test_app_interaction.R),编写常规的shinytest逻辑:

app <- shinytest::ShinyDriver$new(start_golem_app())
# 示例:模拟输入并验证输出
app$setInputs(username = "test_user")
app$expectValue(outputId = "greeting", expected = "Hello test_user!")
app$stop()

步骤3:用testthat封装验证

在tests/testthat下创建testthat测试文件(比如test-shinytest-integration.R),通过expect_pass验证shinytest脚本的执行结果:

test_that("golem app passes shinytest", {
  expect_pass(
    shinytest::testApp(
      appFunc = start_golem_app,
      testnames = "test_app_interaction"  # 对应shinytest脚本的文件名(不含.R)
    )
  )
})

注意事项

  • 确保包已通过devtools::load_all()加载或本地安装,否则无法调用yourpackage::run_app()。
  • testApp的appFunc参数支持直接传入返回Shiny应用对象的函数,完美适配golem的应用启动方式,无需指定传统的app目录。

内容的提问来源于stack exchange,提问作者Stéphane Laurent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 16:58:58