erlangeunit

Erlang eunit test skipped (no code changed!)


I had a failing test. I made some changes, undid them, run my test again. Instead of failing, I get this.

All of my .beam files are present. My git status shows no changes.

=====================================================
  Failed: 0.  Skipped: 0.  Passed: 0.
One or more tests were cancelled.
error
60> room:test().

=ERROR REPORT==== 14-Feb-2017::19:36:57 ===
** Generic server <0.1375.0> terminating
** Last message in was {join,#{auth => "auth",name => "Blandline"}}
** When Server state == {<0.1376.0>,<0.1379.0>,<0.1380.0>}
** Reason for termination ==
** {function_clause,
       [{table,terminate,
            [{{case_clause,{ok,#{x => "Blandline"}}},
              [{table,handle_call,3,[{file,"table.erl"},{line,15}]},
               {gen_server,try_handle_call,4,
                   [{file,"gen_server.erl"},{line,615}]},
               {gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,647}]},
               {proc_lib,init_p_do_apply,3,
                   [{file,"proc_lib.erl"},{line,247}]}]},
             {<0.1376.0>,<0.1379.0>,<0.1380.0>}],
            [{file,"table.erl"},{line,47}]},
        {gen_server,try_terminate,3,[{file,"gen_server.erl"},{line,629}]},
        {gen_server,terminate,7,[{file,"gen_server.erl"},{line,795}]},
        {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]}
room: firstMove_test...*skipped*
undefined
*unexpected termination of test process*
::{function_clause,[{table,terminate,
                           [{{case_clause,{ok,#{x => "Blandline"}}},
                             [{table,handle_call,3,[...]},
                              {gen_server,try_handle_call,4,...},
                              {gen_server,handle_msg,...},
                              {proc_lib,...}]},
                            {<0.1376.0>,<0.1379.0>,<0.1380.0>}],
                           [{file,"table.erl"},{line,47}]},
                    {gen_server,try_terminate,3,
                                [{file,"gen_server.erl"},{line,629}]},
                    {gen_server,terminate,7,
                                [{file,"gen_server.erl"},{line,795}]},
                    {proc_lib,init_p_do_apply,3,
                              [{file,"proc_lib.erl"},{line,247}]}]}

=======================================================
  Failed: 0.  Skipped: 0.  Passed: 0.
One or more tests were cancelled.
error

s:s is just a shortcut for gen_server:call. I'm testing other modules from this module.

-module(room).
-behaviour(gen_server).
-compile(export_all).
-include_lib("eunit/include/eunit.hrl").

testRoom() ->
  {ok, R} = room:go(),
  {ok, T, _} = s:s(R, {join, #{name => "Blandline", auth => "auth"}}),
  {ok, T, _} = s:s(R, {join, #{name => "Kreutzer", auth => "auth"}}),
  {R, T}.

firstMove_test() ->
  {R, T} = testRoom(),
  {ok, #{actions := _, board := _, clock := _}} = s:s(R, {play, T, {take, x, {1,2}}, {"Blandline", "auth"}}).

badMove_test() ->
  {R, T} = testRoom(),
  {error, invalid_input} = s:s(R, {play, T, {take, x, {1,2,3}}, {"Blandline", "auth"}}).

badAuth_test() ->
  {R, T} = testRoom(),
  {error, invalid_auth} = s:s(R, {play, T, {take, x, {1,2}}, {"Blandline", "badauth"}}).

badTable_test() ->
  {R, _} = testRoom(),
  {error, bad_table} = s:s(R, {play, self(), {take, x, {1,2}}, {"Blandline", "badauth"}}).

Solution

  • As the error report is telling you:

    [{{case_clause,{ok,#{x => "Blandline"}}},
        [{table,handle_call,3,[{file,"table.erl"},{line,15}]},
    

    You have, in file table.erl line 15 a case clause that doesn't match :-)

    I think that the reason why the test started failing although you undid your changes is because you are running the tests from the Erlang shell and you have something (like a gen_server) still running that fails for some reasons.

    Said in another way, your tests are not repeatable because either they don't teardown properly all the dependencies or because they require external dependencies like this gen_server to be started by hand in the Erlang shell before running the tests.

    I strongly suggest to forget the Erlang shell to run tests. Use rebar3 to build and run the tests from a terminal. This will force you to take care of all the dependencies and to have robust tests. The time taken to make this work will be well spent :-)