Now I want to test whether the output of this function is indeed ”Hello World!“. How can I realize this using the package Test? In other word, what should the SomeFunction() be in the following code:
using Test
out = SomeFunction(greet())
@test out == "Hello World!"
Correct. If you want your function to be testable, then this is the way to do it. If you don’t want to (or cannot) change the function, you have to resort to a hack, e.g., what @screw_dog suggests, or using a Pipe:
try
p = Pipe()
redirect_stdout(greet, p)
@test String(readavailable(p)) == "Hello world"
finally
close(p)
end
julia> function stdout_to_string(f)
p = Pipe()
redirect_stdout(f, p)
str = String(readavailable(p))
close(p)
return str
end
stdout_to_string (generic function with 1 method)
julia> stdout_to_string(greet)
"Hello World!"