# How to get the output of the function \`print()\`?

**URL:** https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573
**Category:** General Usage
**Tags:** question
**Created:** [February 22, 2024, 11:58am UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573 "2024-02-22T11:58:52Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [February 22, 2024, 11:58am UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/1 "2024-02-22T11:58:52Z")

</div>

For example, I have a function:

```julia
greet() = print("Hello World!")

```

Now I want to test whether the output of this function is indeed ”Hello World!“. How can I realize this using the package `Test`? In other word, what should the `SomeFunction()` be in the following code:

```julia
using Test
out = SomeFunction(greet())
@test out == "Hello World!"

```

Does this make sense?

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [February 22, 2024, 12:02pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/2 "2024-02-22T12:02:25Z")

</div>

You should change your design:

```julia
greet(io) = print(io, "Hello world")
greet() = greet(stdout)

```

That way, you can pass an IO object to write into:

```julia
using Test
io = IOBuffer()
greet(io)
@test String(take!(io)) == "Hello world"

```

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [February 22, 2024, 12:03pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/3 "2024-02-22T12:03:03Z")

</div>

Perhaps [`redirect_stdout`](https://docs.julialang.org/en/v1/base/io-network/#Base.redirect_stdout-Tuple%7BFunction,%20Any%7D) would work. Something like:

```julia
using Test
stream = IOBuffer()
redirect_stdout(greet(), stream)
@test take!(stream) == "Hello World!"

```

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [February 22, 2024, 12:17pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/4 "2024-02-22T12:17:05Z")

</div>

> [@screw\_dog](#):
>
> `redirect_stdout(greet(), stream)`

It tells:  
`ERROR: MethodError: no method matching (::Base.RedirectStdStream)(::Nothing, ::IOBuffer)`

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [February 22, 2024, 12:17pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/5 "2024-02-22T12:17:52Z")

</div>

Oops, should be `redirect_stdout(greet, stream)`

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [February 22, 2024, 12:21pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/6 "2024-02-22T12:21:36Z")

</div>

> [@screw\_dog](#):
>
> redirect\_stdout(greet, stream)

😥 It still reports error:  
`ERROR: MethodError: no method matching (::Base.RedirectStdStream)(::IOBuffer)`

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [February 22, 2024, 12:27pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/7 "2024-02-22T12:27:48Z")

</div>

Thanks! This method does work, but it changes the original function.

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [February 22, 2024, 12:41pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/8 "2024-02-22T12:41:15Z")

</div>

Sorry, haven’t done this for a while.

```julia
using Test

greet() = print("Hello World!")

out = stdout
stream = redirect_stdout()

greet()

redirect_stdout(out)
@test String(readavailable(stream)) == "Hello World!"

```

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [February 22, 2024, 12:44pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/9 "2024-02-22T12:44:02Z")

</div>

Worked. 👍

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [February 22, 2024, 12:44pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/10 "2024-02-22T12:44:55Z")

</div>

> [@WuSiren](#):
>
> This method does work, but it changes the original function.

Correct. If you want your function to be testable, then this is the way to do it. If you don’t want to (or cannot) change the function, you have to resort to a hack, e.g., what @screw_dog suggests, or using a `Pipe`:

```julia
try
    p = Pipe()
    redirect_stdout(greet, p)
    @test String(readavailable(p)) == "Hello world"
finally
    close(p)
end

```

EDIT: added `close`, thanks @mkitti!

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [February 22, 2024, 12:53pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/11 "2024-02-22T12:53:00Z")

</div>

I was just about to suggest that.

```julia-repl
julia> function stdout_to_string(f)
           p = Pipe()
           redirect_stdout(f, p)
           str = String(readavailable(p))
           close(p)
           return str
       end
stdout_to_string (generic function with 1 method)

julia> stdout_to_string(greet)
"Hello World!"

```

---

<div class="post-metadata">

### Author: ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)
#### Post date: [February 22, 2024, 1:09pm UTC](https://discourse.julialang.org/t/how-to-get-the-output-of-the-function-print/110573/12 "2024-02-22T13:09:06Z")

</div>

Thanks to everyone! 🤝 🤝
