using Base.Test
function f(x)
if x < 0
warn("x should be non-negative")
end
return x
end
@test_warning f(-1) #this is the test I am looking for
julia> begin
stderr_orig = STDERR
rd, wr = redirect_stderr()
try
warn("HI")
finally
redirect_stderr(stderr_orig)
close(wr)
out = readall(rd)
println(length(out),out)
end
end
25WARNING: HI
in you example, code, throwing an error does not cause anything to be written to stdout, it is a break in control flow that gets caught by the REPL, skipping execution of all of the rest of your code.
Note that the calls to both redirect_stderr and close are necessary before readall, because we have multiple handles to the write end of the pipe, and need to close all of them before the read end will receive the EOF signal