Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

testthat

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?

like image 669
user2793761 Avatar asked Jul 15 '14 20:07

user2793761


2 Answers

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://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.

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)
})
like image 54
eddies Avatar answered Sep 26 '22 01:09

eddies


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.

like image 38
Gabor Csardi Avatar answered Sep 24 '22 01:09

Gabor Csardi