ExUnit v1.0.5 ExUnit.CaptureIO
Functionality to capture IO for testing.
Examples
defmodule AssertionTest do
use ExUnit.Case
import ExUnit.CaptureIO
test "checking the return value and the IO output" do
fun = fn ->
assert Enum.each(["some", "example"], &(IO.puts &1)) == :ok
end
assert capture_io(fun) == "some\nexample\n"
# Or use only: `capture_io(fun)` to silence the
# IO output (so only assert the return value)
end
end
Summary
Functions
Captures IO generated when evaluating fun
Functions
Captures IO generated when evaluating fun
.
Returns the binary which is the captured output.
By default, capture_io
replaces the group_leader
(:stdio
)
for the current process. However, the capturing of any other
named device, such as :stderr
, is also possible globally by
giving the registered device name explicitly as an argument.
Note that when capturing something other than :stdio
,
the test should run with async false.
When capturing :stdio
, if the :capture_prompt
option is false
,
prompts (specified as arguments to IO.get*
functions) are not
captured.
A developer can set a string as an input. The default
input is :eof
.
Examples
iex> capture_io(fn -> IO.write "john" end) == "john"
true
iex> capture_io(:stderr, fn -> IO.write(:stderr, "john") end) == "john"
true
iex> capture_io("this is input", fn ->
...> input = IO.gets ">"
...> IO.write input
...> end) == ">this is input"
true
iex> capture_io([input: "this is input", capture_prompt: false], fn ->
...> input = IO.gets ">"
...> IO.write input
...> end) == "this is input"
true