简体   繁体   English

R testthat包:如何在使用test_file()时看到message()的输出

[英]R testthat package: How can I see output from message() when using test_file()

I am using the excellent testthat package in R. My issue is that I cannot see any output from the message() function in the code being tested when using test_file. 我在R中使用了优秀的testthat包。我的问题是,在使用test_file时,我无法在正在测试的代码中看到message()函数的任何输出。 for example, say I have following code in a file called test_message.R 例如,假设我在名为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)
 })

I run test_file as follows and get the output below 我按如下方式运行test_file并获得下面的输出

> test_file("test_message.R")
Cat: Hello world
.

So I am not seeing the text from message(). 所以我没有看到来自message()的文本。

However, when I run the code on its own I do see it: 但是,当我自己运行代码时,我确实看到了:

> f()
Message: Hello world
Cat: Hello world
[1] 1

I know that by default, message() writes to stderr and cat writes to stdout and I'm guessing that test_file "intercepts" stderr to test for the text in warnings and errors. 我知道默认情况下,message()写入stderr和cat写入stdout,我猜测test_file“拦截”stderr来测试警告和错误中的文本。 Is there any way I can configure things so I see message() text on the console? 有什么方法可以配置东西,所以我在控制台上看到message()文本?

This is my inaugural post to SO, on a language I just started learning two weeks ago, so be gentle. 这是我在SO上的首次发表的帖子,这是我两周前开始学习的一种语言,所以请保持温和。

I believe the following is what you're looking for: 我相信您正在寻找以下内容:

test_that ("message shows up", {
  expect_message(f(), "^Message: Hello world\\n")
})

Also, see the documentation for shows_message & expect_message on page 23 of testthat.pdf (For future reference: published 2014-02-22 00:25:04 for v0.8.1). 另外,请参阅该文档shows_messageexpect_message第23页testthat.pdf (以供将来参考:对于v0.8.1发布的2014年2月22日0时25分04秒)。

And full credit to GSee for https://stackoverflow.com/a/24476491/3811916 which is the second post after this one that I turned up when looking to test for this same issue. 并且完全归功于GSee for https://stackoverflow.com/a/24476491/3811916 ,这是我在考虑同样的问题时出现的第二篇文章。

I just discovered evaluate_promise (also in the pdf linked above). 我刚刚发现了evaluate_promise (也在上面链接的pdf中)。 So here's an alternative that will test for the message and print the output: 所以这是一个替代方案,它将测试消息并打印输出:

test_that ("message shows up", {
  result <- evaluate_promise(f(), print = TRUE)
  expect_that(result$message, equals("Message: Hello world\n"))
  print(result$output)
})

You can't do this, at least not without modifying the source code of testthat . 你不能这样做,至少在没有修改testthat源代码的情况下也是testthat This is the part that runs the code of the test: https://github.com/hadley/testthat/blob/0af22cfc7c7f6b13b02537f0d37d96d72d8a98b7/R/test-that.r#L65 If you remove the suppressMessages from the test_code function, then the messages will be displayed. 这是运行测试代码的部分: https//github.com/hadley/testthat/blob/0af22cfc7c7f6b13b02537f0d37d96d72d8a98b7/R/test-that.r#L65如果从test_code函数中删除suppressMessages ,则消息将显示。

Btw. 顺便说一句。 testthat does not capture standard output or error AFAIK, so if you write to it using cat or some other way, that will also displayed. testthat 捕获标准输出或错误AFAIK,所以如果你使用写它cat或其他方式,这也将显示出来。

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

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