Skip to content

testthat::test_that() : output of utils::capture.output() is different outside and inside testthat::test_that() #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gael-millot opened this issue Apr 10, 2025 · 2 comments

Comments

@gael-millot
Copy link

gael-millot commented Apr 10, 2025

Hello.

I do not understand this behavior. Is it a bug ?
If not, how to solve this?
Thanks !
Gael.

In the follwing code, print(output) should return "HELLO", not character(0):

testthat::test_that(
    desc = "get_message", 
    code = {
        output <- utils::capture.output(
            message('HELLO'),
            type = "message"
        )
        print(output)
    }
)

character(0)
── Skip: get_message ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Reason: empty test

output <- utils::capture.output(
    message('HELLO'),
    type = "message"
)
print(output)

[1] "HELLO"

Another more complicate example:

fun1 <- function(data){
    output <- utils::capture.output(
        base::suppressWarnings(expr = base::eval(expr = base::parse(text = data)), classes = "warning"),
        type = "message"
    )
    return(output)
}
str2 <- "message('HELLO')"
a <- fun1(str2)
print(a)

[1] "HELLO"

testthat::test_that(
    desc = "get_message", 
    code = {
        str2 <- "message('HELLO')"
        a <- fun1(str2)
        print(a)
    }
)
# or testthat::test_that(desc = "get_message", code = print(fun1(str2)))

character(0)
── Skip: get_message ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Reason: empty test

@gael-millot gael-millot changed the title testthat::test_that() : output of a function using utils::capture.output() is different outside ans inside testthat::test_that() testthat::test_that() : output of a function using utils::capture.output() is different outside and inside testthat::test_that() Apr 10, 2025
@gael-millot
Copy link
Author

gael-millot commented Apr 10, 2025

Here, results are not different:

fun2 <- function(data){
    output <- base::eval(expr = base::parse(text = data))
    return(output)
}
str2 <- "sum(1:3)"
a <- fun2(str2)
print(a)

[1] 6

testthat::test_that("get_message()", {
    a <- fun2(str2)
    print(a)
})

[1] 6
── Skip: get_message() ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Reason: empty test

@gael-millot gael-millot changed the title testthat::test_that() : output of a function using utils::capture.output() is different outside and inside testthat::test_that() testthat::test_that() : output of utils::capture.output() is different outside and inside testthat::test_that() Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant