我在 R 中使用了出色的 testthat 包。我的问题是,在使用 test_file 时,我在正在测试的代码中看不到 message() 函数的任何输出。例如,假设我在名为 test_message.R 的文件中有以下代码
f <- function() {
message ("Message: Hello world")
cat ("Cat: Hello world\n")
return (1)
}
test_that ("message shows up", {
expect_equal(f(), 1)
})
我按如下方式运行 test_file 并得到下面的输出
> test_file("test_message.R")
Cat: Hello world
.
所以我没有看到来自 message() 的文本。
但是,当我自己运行代码时,我确实看到了它:
> f()
Message: Hello world
Cat: Hello world
[1] 1
我知道默认情况下,message() 写入 stderr,而 cat 写入 stdout,我猜测 test_file “拦截”stderr 以测试警告和错误中的文本。有什么方法可以让我在控制台上看到 message() 文本?