【问题标题】:How to test standalone R scripts which are part of a package using testthat如何使用 testthat 测试作为包一部分的独立 R 脚本
【发布时间】:2018-02-02 23:39:09
【问题描述】:

我正在构建一个 R 包,RE,我想用它打包一个独立的脚本,放在 RE/exec/wrapper.R 中。我为 testthat 编写了一个测试函数,它在使用devtools::test("RE") 从命令行运行时运行良好,但在使用devtools::check("RE") 运行时失败。

失败的相关代码部分是:

cmd <- "Rscript \"C:\\RE\\exec\\wrapper.R\" base print \"{\\\"x\\\": 2}\""
rv <- system(cmd, intern = FALSE, ignore.stderr = FALSE, show.output.on.console = FALSE)

当作为“测试”的一部分运行时,一切都运行良好,system 返回0 应该如此。当作为“检查”的一部分运行时,system 给出下面给出的错误消息并返回1

Error in file(filename, "r", encoding = encoding) : cannot open the connection
Calls: local ... eval.parent -> eval -> eval -> eval -> eval -> source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) : cannot open file 'startup.Rs': No such file or directory
Execution halted

我有:

  1. 检查文件是否存在(在两种情况下都存在)
  2. 试过tryCatch。没有发现错误,system 只返回一个1

这让我觉得这是systemR CMD CHECK 的问题。

【问题讨论】:

    标签: r testthat


    【解决方案1】:

    答案在https://github.com/r-lib/testthat/issues/144 中。环境变量 R_TESTS 必须通过顶级测试脚本 (package_root/tests/testthat.R) 中的 Sys.setenv("R_TESTS" = "") 取消设置,否则从 R CMD CHECK 调用时 system 将无法正确运行.我把这个问题留了下来,因为我花了几个小时的试验和搜索才找到答案。

    【讨论】:

      猜你喜欢
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2010-11-11
      相关资源
      最近更新 更多