How to use @test_warn?

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

               _
   _       _ _(_)_     |  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?

2 Likes

See @test_warn doesn't work with @warn in 0.7. TL;DR: Use @test_logs instead.

3 Likes
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.

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

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
and while running

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??

Probably a mistake.

1 Like

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

because of Logging @warn no longer prints to stderr