[erlang-questions] ANN mock_io (mock Erlang I/O protocol) version 0.1.0

Marco Molteni marco.molteni@REDACTED
Mon May 8 14:00:39 CEST 2017


Hello,

I am happy to announce mock_io, https://bitbucket.org/marco_m/mock_io

A simple mock of the Erlang I/O protocol, to allow unit testing code that performs I/O operations (`io:fwrite`, `io:fread`, `file:read`, `file:write`, ...).

By default, it mocks the `standard_io` device by manipulating the group leader, but you can mock any device/file.

Focuses on simplicity and allowing painless mocking of I/O, not on performance.

## Usage examples

### Extracting output from UUT

UUT:
```erlang
-module(uut).
write_to_stdout() ->
io:fwrite("~p ~p ~s~n", [1, a, "ciao"]).
```

EUnit test:

```erlang
capture_stdout_test() ->
{IO, GL} = mock_io:setup(),
uut:write_to_stdout(),
?assertEqual(<<"1 a ciao\n">>, mock_io:extract(IO)),
mock_io:teardown({IO, GL}).
```

### Injecting input to UUT

UUT:

```erlang
-module(uut).
read_from_stdin() ->
io:get_line("prompt").
```

EUnit test:

```erlang
inject_to_stdin_test() ->
{IO, GL} = mock_io:setup(),
mock_io:inject(IO, <<"pizza pazza puzza\n">>),
?assertEqual("pizza pazza puzza\n", uut:read_from_stdin()),
?assertEqual(<<>>, mock_io:remaining_input(IO)),
mock_io:teardown({IO, GL}).
```

Hope people find this useful :-)
marco


More information about the erlang-questions mailing list