# How to use @test\_warn?

**URL:** https://discourse.julialang.org/t/how-to-use-test-warn/15557
**Category:** General Usage
**Tags:** question
**Created:** [September 27, 2018, 1:11pm UTC](https://discourse.julialang.org/t/how-to-use-test-warn/15557 "2018-09-27T13:11:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cstook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstook/32/3731_2.png) [@cstook](https://discourse.julialang.org/u/cstook)
#### Post date: [September 27, 2018, 1:11pm UTC](https://discourse.julialang.org/t/how-to-use-test-warn/15557/1 "2018-09-27T13:11:13Z")

</div>

I don’t understand how to use @test\_warn. MWE of my problem is below.

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.0.0 (2018-08-08)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using Test

julia> function foo2(x)
           @warn "you have been warned"
           x
       end
foo2 (generic function with 1 method)

julia> @test_warn "you have been warned" foo2(3)
┌ Warning: you have been warned
└ @ Main REPL[2]:2
Test Failed at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Test\src\Test.jl:609
  Expression: contains_warn(read(fname, String), $(Expr(:escape, "you have been warned")))
ERROR: There was an error during testing

julia> @test_warn (x)->begin;println("got:",x,":");true;end foo2(3)
┌ Warning: you have been warned
└ @ Main REPL[2]:2
got::
3

```

What is the correct way to use @test\_warn?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [September 27, 2018, 1:25pm UTC](https://discourse.julialang.org/t/how-to-use-test-warn/15557/2 "2018-09-27T13:25:29Z")

</div>

See [@test\_warn doesn't work with @warn in 0.7](https://discourse.julialang.org/t/test-warn-doesnt-work-with-warn-in-0-7/9001). TL;DR: Use `@test_logs` instead.

---

<div class="post-metadata">

### Author: ![cstook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstook/32/3731_2.png) [@cstook](https://discourse.julialang.org/u/cstook)
#### Post date: [September 27, 2018, 1:38pm UTC](https://discourse.julialang.org/t/how-to-use-test-warn/15557/3 "2018-09-27T13:38:15Z")

</div>

```julia
julia> @test_logs (:warn,"you have been warned") foo2(3)
3

julia>

```

Much better. I thought I had tried that. I guess I didn’t.  
Thank You.

---

<div class="post-metadata">

### Author: ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)
#### Post date: [November 5, 2018, 12:55pm UTC](https://discourse.julialang.org/t/how-to-use-test-warn/15557/4 "2018-11-05T12:55:12Z")

</div>

why is then `@test_warn` not deprecated?  
I’ve just run into the same problem:

```julia
julia> function foo(x)
                  @warn "you have been warned"
                  x
              end
julia> @test_warn "you have been warned" foo(2)
┌ Warning: you have been warned
└ @ Main REPL[27]:2
Test Failed at /build/julia/src/julia/usr/share/julia/stdlib/v1.0/Test/src/Test.jl:609
  Expression: contains_warn(read(fname, String), $(Expr(:escape, "you have been warned")))
ERROR: There was an error during testing
julia> @test_logs (:warn, "you have been warned") foo(2)
2

```

I had a look at [julia/Test.jl at 099979931ce1e666e15ba090cf257bd4fbae8ede · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/099979931ce1e666e15ba090cf257bd4fbae8ede/stdlib/Test/src/Test.jl#L600)  
and while running

```julia
ret = open(fname, "w") do f
         redirect_stderr(f) do
            foo(2)
         end
       end;
       read(fname, String)

```

nothing is written to `stderr`, so `@test_warn` couldn’t possibly work??

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [November 5, 2018, 2:07pm UTC](https://discourse.julialang.org/t/how-to-use-test-warn/15557/5 "2018-11-05T14:07:11Z")

</div>

> [@abulak](#):
>
> why is then `@test_warn` not deprecated?

Probably a mistake.

---

<div class="post-metadata">

### Author: ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)
#### Post date: [November 5, 2018, 3:49pm UTC](https://discourse.julialang.org/t/how-to-use-test-warn/15557/6 "2018-11-05T15:49:16Z")

</div>

@kristoffer.carlsson not that I am the first one to notice the issue  
[https://github.com/JuliaLang/julia/issues/25612](https://github.com/JuliaLang/julia/issues/25612)

because of Logging @warn no longer prints to `stderr`
