# Io:fwrite does not display anything inside eunit code

**URL:** <https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045>\
**Category:** Questions / Help\
**Tags:** rebar3, eunit\
**Created:** [November 15, 2022, 9:30am UTC](https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045 "2022-11-15T09:30:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhuvan](https://erlangforums.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bhuvan](https://erlangforums.com/u/bhuvan)\
**Post date:** [November 15, 2022, 9:30am UTC](https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045/1 "2022-11-15T09:30:19Z")

</div>

My eunit code` xyz_test_()` has many lines of code. For example, it calls a function to find out the expected reply, before comparing it with the actual reply using the ?assertEqual macro.

I have tried to add io:fwrite inside the eunit code for debugging. But I do not see any output written by io:fwrite. What could be the reason?

I have put an example code below.

```erlang
-module(my_sort).
-export([sort/1]).
-include_lib("eunit/include/eunit.hrl").

-spec sort([T]) -> [T].
sort([]) -> [];
sort([P|Xs]) ->
  sort([X || X <- Xs, X < P]) ++ [P] ++ sort([X || X <- Xs, P < X]).

sort2_test_() ->
  [ {"testcase with 4 paras", fun test_four/0},
    {"testcase with 5 paras", fun test_five/0}].

test_four() ->
  io:fwrite("Started test_4\n"),
  [?_assertEqual([1,2,3,4], sort([3,1,4,2]))].
test_five() ->
  io:fwrite("Started test_5\n"),
  [?_assertEqual([1,2,3,4,5], sort([3,1,4,2,5]))].

```

---

<div class="post-metadata">

**Author:** ![fizfaz](https://erlangforums.com/user_avatar/erlangforums.com/fizfaz/32/1224_2.png) [@fizfaz](https://erlangforums.com/u/fizfaz)\
**Post date:** [November 15, 2022, 12:01pm UTC](https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045/2 "2022-11-15T12:01:03Z")

</div>

> [@bhuvan](#):
>
> I have tried to add io:fwrite inside the eunit code for debugging. But I do not see any output written by io:fwrite. What could be the reason?

this is explained in the [eunit user-guide](https://www.erlang.org/doc/apps/eunit/chapter.html#EUnit_captures_standard_output):

> **EUnit captures standard output**

> If your test code writes to the standard output, you may be surprised to see that the text does not appear on the console when the tests are running. This is because EUnit captures all standard output from test functions (this also includes setup and cleanup functions, but not generator functions), so that it can be included in the test report if errors occur. To bypass EUnit and print text directly to the console while testing, you can write to the user output stream, as in io:format(user, “~w”, [Term]). The recommended way of doing this is to use the EUnit [Debugging macros](https://www.erlang.org/doc/apps/eunit/chapter.html#Debugging_macros), which make it much simpler.

changing your code to use `?debugMsg`:

```erlang
-module(my_sort).
-export([sort/1]).
-include_lib("eunit/include/eunit.hrl").

-spec sort([T]) -> [T].
sort([]) -> [];
sort([P|Xs]) ->
  sort([X || X <- Xs, X < P]) ++ [P] ++ sort([X || X <- Xs, P < X]).

sort2_test_() ->
  [ {"testcase with 4 paras", fun test_four/0},
    {"testcase with 5 paras", fun test_five/0}].

test_four() ->
  ?debugMsg("Started test_4\n"),
  [?_assertEqual([1,2,3,4], sort([3,1,4,2]))].
test_five() ->
  ?debugMsg("Started test_5\n"),  
  [?_assertEqual([1,2,3,4,5], sort([3,1,4,2,5]))].

```

shows the following output:

```erlang
% rebar3 eunit                                                                       
===> Verifying dependencies...                                                                            
===> Analyzing applications...                                                                            
===> Compiling mylib                                                                                      
===> Performing EUnit tests...                                                                            
/private/tmp/mylib/test/my_sort.erl:15:<0.173.0>: Started test_4                                          
                                                                                                          
/private/tmp/mylib/test/my_sort.erl:18:<0.173.0>: Started test_5                                          
                                                                                                          
..                                                                                                        
Finished in 0.020 seconds                                                                                 
2 tests, 0 failures        

```

---

<div class="post-metadata">

**Author:** ![bhuvan](https://erlangforums.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bhuvan](https://erlangforums.com/u/bhuvan)\
**Post date:** [November 15, 2022, 1:51pm UTC](https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045/3 "2022-11-15T13:51:10Z")

</div>

Thanks, Felix. It worked.

Another query. What about the text specified in sort2\_test\_()\>

```erlang
sort2_test_() ->
  [ {"testcase with 4 paras", fun test_four/0},
    {"testcase with 5 paras", fun test_five/0}].

```

---

<div class="post-metadata">

**Author:** ![fizfaz](https://erlangforums.com/user_avatar/erlangforums.com/fizfaz/32/1224_2.png) [@fizfaz](https://erlangforums.com/u/fizfaz)\
**Post date:** [November 15, 2022, 5:15pm UTC](https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045/4 "2022-11-15T17:15:32Z")

</div>

> [@bhuvan](#):
>
> ```erlang
> sort2_test_() ->
> [ {"testcase with 4 paras", fun test_four/0},
> {"testcase with 5 paras", fun test_five/0}].
> 
> ```

thats the “title”, see [Erlang -- EUnit - a Lightweight Unit Testing Framework for Erlang](https://www.erlang.org/doc/apps/eunit/chapter.html#Titles)

Since this is tagged with rebar3, for example if you use verbose flag you see it (or if something fails):

```erlang
% rebar3 eunit -v                                                         
===> Verifying dependencies...                                                                            
===> Analyzing applications...                                                                            
===> Compiling mylib                                                                                      
===> Performing EUnit tests...                                                                            
======================== EUnit ========================                                                   
file "mylib.app"                                                                                          
  application 'mylib'                                                                                     
    module 'mylib'                                                                                        
    [done in 0.001 s]                                                                                     
  [done in 0.002 s]                                                                                       
module 'my_sort'                                                                                          
  my_sort: fun.test_four (testcase with 4 paras)...ok                                                     
  my_sort: fun.test_five (testcase with 5 paras)...ok                                                     
                          ^^^^^^^^^^^^^^^^^^^^^
  [done in 0.006 s]                                                                                       
=======================================================                                                   
  2 tests passed.                                                 

```

(highlighed with `^^^^…` 🙂 )

---

<div class="post-metadata">

**Author:** ![bhuvan](https://erlangforums.com/letter_avatar_proxy/v4/letter/b/f4b2a3/32.png) [@bhuvan](https://erlangforums.com/u/bhuvan)\
**Post date:** [November 16, 2022, 7:22am UTC](https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045/5 "2022-11-16T07:22:23Z")

</div>

Thanks, Felix. It worked.

---

<div class="post-metadata">

**Author:** ![eproxus](https://erlangforums.com/user_avatar/erlangforums.com/eproxus/32/452_2.png) [@eproxus](https://erlangforums.com/u/eproxus)\
**Post date:** [November 22, 2022, 8:22am UTC](https://erlangforums.com/t/io-fwrite-does-not-display-anything-inside-eunit-code/2045/6 "2022-11-22T08:22:49Z")

</div>

Another useful trick is to prefix the `io` functions with the special IO device `user` which goes to the top group leader IO device:

```erlang
io:format(user, "Started test_4\n", [])

```

That bypasses most (all?) of such IO-redirecting magic.
