R testthat package: How can I see output from message() when using test_file()
Asked Answered
G

2

8

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. for example, say I have following code in a file called 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_message.R")
Cat: Hello world
.

So I am not seeing the text from 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. Is there any way I can configure things so I see message() text on the console?

Greenleaf answered 15/7, 2014 at 20:47 Comment(2)
Can I ask why you want to see the output of message while running the tests?Anachronistic
I put the message() calls in the code in the first place to log the code's progress. I'd like to see those messages while running tests to know how far we got till the code failed, and to see that the messages themselves are working properly. Looks like I will need to use another mechanism for thisGreenleaf
G
5

You can't do this, at least not without modifying the source code of 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.

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.

Gam answered 16/7, 2014 at 0:32 Comment(0)
G
13

This is my inaugural post to SO, on a language I just started learning two weeks ago, so be gentle.

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).

And full credit to GSee for https://mcmap.net/q/1323818/-test-for-exact-string-in-testthat which is the second post after this one that I turned up when looking to test for this same issue.

I just discovered evaluate_promise (also in the pdf linked above). 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)
})
Goldstone answered 23/7, 2014 at 12:40 Comment(2)
Thanks. That is perfect for testing for the message I am looking for. The problem is that I had wanted to actually see the messages printing as they would when running, not just test they exist. I can live without this ability and either use your suggestion to test for messages or just observe messages in a non-testthat environment.Greenleaf
@Greenleaf added an alternative that uses evaluate_promise which might satisfy.Goldstone
G
5

You can't do this, at least not without modifying the source code of 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.

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.

Gam answered 16/7, 2014 at 0:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.