Getting test results from Eunit Erlang

I work with Erlang and EUnit to conduct unit tests, and I would like to write a test leader to automate my unit tests. The problem is that eunit: test / 1 seems to return β€œerror” or β€œnormal”, not a list of tests and what they returned in terms of what passed or failed.

So, is there a way to run tests and return some form of data structure of what tests were performed and their pass / fail state?

+4
source share
3 answers

If you use reinforcement, you do not need to implement your own runner. You can simply run:

rebar eunit 

Rebar will compile and run all the tests in the test directory (as well as the eunit tests inside your modules). In addition, the reinforcement allows you to set the same parameters in rebar.config as in the shell:

 {eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}. 

You can also use these options in the shell:

 > eunit:test([foo], [verbose, {report,{eunit_surefire,[{dir,"."}]}}]). 

See also the documentation for the detailed version and structured report .

An alternative would be to use the Common Test instead of Eunit. The Common Test comes with a runner ( ct_run command) and gives you more flexibility in your test setup, but also a little harder to use. Common Test lacks available macros, but gives very clear html reports.

+9
source

There is no easy or documented way, but there are currently two ways to do this. One of them is to give the option "event_log" when running the tests:

 eunit:test(my_module, [event_log]) 

(this is undocumented and is really intended for debugging only). The resulting "eunit-events.log" file is a text file that Erlang can read using the file: consult (file name).

A more powerful way (and not quite so complicated) is to implement a custom event listener and provide it as an eunit feature:

 eunit:test(my_module, [{report, my_listener_module}]) 

This is not yet documented, but it should be. The listener module implements the behavior of eunit_listener (see Src / eunit_listener.erl). There are only five callback functions. Take a look at the examples src / eunit_tty.erl and src / eunit_surefire.erl.

+6
source

I just clicked on GitHub a very trivial listener that stores EUnit results in a DETS table. This can be useful if you need to continue processing this data, as it is stored in Erlang terms in the DETS table.

https://github.com/prof3ta/eunit_terms

Usage example:

 > eunit:test([fact_test], [{report,{eunit_terms,[]}}]). All 3 tests passed. ok > {ok, Ref} = dets:open_file(results). {ok,#Ref<0.0.0.114>} > dets:lookup(Ref, testsuite). [{testsuite,<<"module 'fact_test'">>,8,<<>>,3,0,0,0, [{testcase,{fact_test,fact_zero_test,0,0},[],ok,0,<<>>}, {testcase,{fact_test,fact_neg_test,0,0},[],ok,0,<<>>}, {testcase,{fact_test,fact_pos_test,0,0},[],ok,0,<<>>}]}] 

Hope this helps.

+2
source

All Articles