# Suppress printing in Unit test

**URL:** <https://discourse.julialang.org/t/suppress-printing-in-unit-test/13245>\
**Category:** General Usage\
**Tags:** testing\
**Created:** [August 11, 2018, 1:35pm UTC](https://discourse.julialang.org/t/suppress-printing-in-unit-test/13245 "2018-08-11T13:35:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)\
**Post date:** [August 11, 2018, 1:35pm UTC](https://discourse.julialang.org/t/suppress-printing-in-unit-test/13245/1 "2018-08-11T13:35:26Z")

</div>

Let’s say I have a function that is just used to print some information on the screen, like:

```julia
function printStuff(x)
  print("Round(x=$(x)) = $(round(x,digits=2)) ")
  nothing
end

```

As this function is used in a larger code base, I would like to add a test that checks that the function prints without any errors (e.g. that the function still works after a new Julia version is released).

I could add

```julia
@test printStuff(4.1123) == nothing

```

to my unit tests.

However, I would like to prevent the function from actually printing to the screen. Is there any way to do this?

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [August 11, 2018, 1:52pm UTC](https://discourse.julialang.org/t/suppress-printing-in-unit-test/13245/2 "2018-08-11T13:52:22Z")

</div>

You could use one of the new logging macros i.e. `@info` and then use `@test_logs` to test your function.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 11, 2018, 2:04pm UTC](https://discourse.julialang.org/t/suppress-printing-in-unit-test/13245/3 "2018-08-11T14:04:08Z")

</div>

Could also turn `printStuff(x)` into `printStuff(io::IO, x)` (to match `Base.print`) and then use an `IOBuffer` (or `devnull`) in your tests, similar to

```julia
julia> buf = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> print(buf, "abcd")

julia> String(take!(buf))
"abcd"

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [August 11, 2018, 2:19pm UTC](https://discourse.julialang.org/t/suppress-printing-in-unit-test/13245/4 "2018-08-11T14:19:53Z")

</div>

Another option is to use [Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl).

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [August 11, 2018, 3:39pm UTC](https://discourse.julialang.org/t/suppress-printing-in-unit-test/13245/5 "2018-08-11T15:39:44Z")

</div>

Echoing @pfitzseb, Suppressor.jl is designed for just this purpose, and recently got updated to also redirect the new logging macros. You can also capture the output and compare it to an expected result if you want to test what actually got printed.
