繁体   English   中英

如何使用 testthat 测试作为包一部分的独立 R 脚本

[英]How to test standalone R scripts which are part of a package using testthat

我正在构建一个 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM