# Unit test for f()=print("something")

**URL:** https://discourse.julialang.org/t/unit-test-for-f-print-something/9876
**Category:** General Usage
**Created:** [March 22, 2018, 3:39am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876 "2018-03-22T03:39:28Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [March 22, 2018, 3:39am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/1 "2018-03-22T03:39:28Z")

</div>

I would like to write unit tests like the following

```julia
f() = print("something")
@test print_to_string(f) == "something" 

```

I’m not able to successfully define the function `print_to_string`. I’m getting lost while searching the internet including this Julia discourse forum for solutions. There is talk of “redirecting\_stdout”, “blocking”, “tasks”, “channels”, etc.

Any suggestions for how to define the function `print_to_string`?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 22, 2018, 3:55am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/2 "2018-03-22T03:55:01Z")

</div>

Try `@capture_out` from [https://github.com/JuliaIO/Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl)

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [March 22, 2018, 4:02am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/3 "2018-03-22T04:02:00Z")

</div>

I looked at that, but I couldn’t figure out what `block` should contain, or how to get my hands on the thing that is being captured. I got the impression that the output of `block` was simply being thrown in the “bit bucket”, that is, being suppressed. Did I misunderstand how @capture\_out actually works?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 22, 2018, 4:31am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/4 "2018-03-22T04:31:23Z")

</div>

`@capture_out` just returns whatever was printed by the captured expression, e.g.:

```julia
julia> using Suppressor

julia> f() = print("something")
f (generic function with 1 method)

julia> result = @capture_out f()
"something"

julia> result
"something"

```

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [March 22, 2018, 4:42am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/5 "2018-03-22T04:42:14Z")

</div>

aha. Thank you.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 22, 2018, 7:29am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/6 "2018-03-22T07:29:52Z")

</div>

Also, if you are testing `show` methods, `repr` may be useful.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 22, 2018, 12:14pm UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/7 "2018-03-22T12:14:20Z")

</div>

You can look at the [`@test_warn` macro in Base](https://github.com/JuliaLang/julia/blob/33fdd327728894d4987005322b121282bf8f1b99/stdlib/Test/src/Test.jl#L541-L568) and do something similar except for stdout instead of stderr.

Alternatively, consider whether it makes sense to pass an `io` argument to your function(s) so that you can direct your output to an arbitrary stream.

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [March 23, 2018, 12:37am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/8 "2018-03-23T00:37:22Z")

</div>

Update for those who run into similar issues:

```julia
julia> versioninfo()
Julia Version 0.6.2
Commit d386e40c17* (2017-12-13 18:08 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)

```

```julia
julia> @test_warn "something" error("something")
unlink: resource busy or locked (EBUSY)

```

This is caused by the `rm(fname, force=true)` statement in macro `test_warn`. A web search tells me that it happens on Windows 10 and is either caused by the anti-virus program or by not running Jupyter as adminstrator. I don’t want to turn off the anti-virus program nor do I want to run Julia as `administrator`

Therefore I tried

```julia
julia> using Suppressor
julia> f() = print("something")
julia> result = @capture_out f()
MethodError: no method matching redirect_stdout(::IJulia.IJuliaStdio{Base.PipeEndpoint})

```

No good either. So the solution that works for me is the following.

```julia
print_to_string(f::Function, args...) = begin
    io = IOBuffer()
    f(io,args...)
    String(take!(io))
end

f(io::IO) = print(io, "something")

julia> print_to_string(f) == "something"
true

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [March 23, 2018, 4:05am UTC](https://discourse.julialang.org/t/unit-test-for-f-print-something/9876/9 "2018-03-23T04:05:19Z")

</div>

The issue you’re seeing with Suppressor on IJulia should hopefully be resolved by [https://github.com/JuliaLang/IJulia.jl/pull/641](https://github.com/JuliaLang/IJulia.jl/pull/641)
