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

使用usethis::use_test()创建Shiny模块单元测试文件报错求助

解决Shiny模块测试的问题

错误原因

usethis::use_test()是为R包项目设计的工具,它要求当前打开的文件必须位于R包的R/目录下。你的应用是单文件app.R形式的Shiny项目,不符合R包结构要求,因此触发该错误。

两种可行的测试方案

方案1:手动搭建测试目录(无需转为R包)

不需要依赖usethis,直接手动创建测试结构:

  • 在项目根目录创建tests/testthat/文件夹
  • 在该文件夹下新建测试文件(如test-dataselect.R、test-plot.R),手动编写测试代码

方案2:转为R包结构(推荐,更规范易维护)

  1. 用usethis::create_package("my_shiny_app")初始化一个R包项目
  2. 拆分代码到对应目录:
    • 将dataselect_ui、dataselect_server放到R/dataselect_module.R
    • 将plot_ui、plot_server放到R/plot_module.R
    • 主应用的ui、server和shinyApp调用保留在R/app.R
  3. 运行usethis::use_testthat()初始化测试框架,之后就可以正常使用usethis::use_test("dataselect")创建测试文件

模块测试用例示例(基于shinytest2)

先安装必要依赖:

install.packages(c("testthat", "shinytest2"))

测试dataselect模块

创建tests/testthat/test-dataselect.R:

library(testthat)
library(shiny)
library(shinytest2)
library(DT)
library(reshape2)

# 加载模块代码(如果是R包结构,直接library(my_shiny_app)即可)
source("R/dataselect_module.R")

test_that("dataselect模块交互逻辑符合预期", {
  # 启动独立的测试应用
  app <- AppDriver$new(
    shinyApp(
      ui = dataselect_ui("test"),
      server = function(input, output, session) {
        dataselect_server("test")
      }
    )
  )
  
  # 验证初始状态:Name选择框为空
  app$expect_value(input = "test-Name", value = "")
  
  # 切换Nametype为Name1,检查Name选项是否正确加载
  app$set_inputs(`test-Nametype` = "Name1")
  app$expect_value(
    input = "test-Name",
    choices = c("Aix galericulata", "Grus grus", "    Alces alces")
  )
  
  # 选择Name1的第一个选项,验证输出数据
  app$set_inputs(`test-Name` = "Aix galericulata")
  final_df <- app$get_value(output = "test-tab")$data
  expect_equal(nrow(final_df), 1)
  expect_equal(final_df$Name1, "Aix galericulata")
  
  # 切换Nametype为Name2并选择Elk,验证数据匹配
  app$set_inputs(`test-Nametype` = "Name2")
  app$set_inputs(`test-Name` = "Elk")
  final_df <- app$get_value(output = "test-tab")$data
  expect_equal(final_df$individualCount, 1)
  
  # 关闭测试应用
  app$stop()
})

测试plot模块

创建tests/testthat/test-plot.R:

library(testthat)
library(shiny)
library(shinytest2)
library(plotly)
library(reshape2)

# 加载模块代码
source("R/dataselect_module.R")
source("R/plot_module.R")

test_that("plot模块根据输入生成正确图表", {
  # 模拟dataselect模块的输出(替代实际模块依赖)
  mock_finalDf <- reactive({
    data.frame(
      Name1 = "Aix galericulata",
      Name2 = "Mandarin Duck",
      eventDate = "2015-03-11",
      individualCount = 1
    )
  })
  mock_input_Name <- reactive("Aix galericulata")
  
  # 启动测试应用
  app <- AppDriver$new(
    shinyApp(
      ui = plot_ui("test"),
      server = function(input, output, session) {
        plot_server("test", input_Name = mock_input_Name, finalDf = mock_finalDf)
      }
    )
  )
  
  # 验证图表输出存在
  app$expect_output("test-plot")
  
  # 提取图表数据,验证内容正确性
  plot_data <- app$get_value(output = "test-plot")$x$data[[1]]
  expect_equal(plot_data$x, "2015-03-11")
  expect_equal(plot_data$y, 1)
  
  # 关闭测试应用
  app$stop()
})

运行测试

在R控制台执行以下命令运行所有测试:

testthat::test_dir("tests/testthat/")

内容的提问来源于stack exchange,提问作者Maryam Momeni

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 04:20:20