Do not print exception stack trace in EUnit

I am writing a test with EUnit, but it doesn’t output anything to the console.

exp_test() -> ?assertEqual(0, 1/0). 

Run this module: exp_test () in the output of the Erlang shell after

 ** exception error: bad argument in an arithmetic expression in function exp_test:'-exp_test/0-fun-0-'/1 (src/test/eunit/xxx_test.erl, line 8) 

But in EUnit output after

 > eunit:test(xxx). > xxx_test: exp_test...*failed* ::badarith 

EUnit does not display exception tracing information

I am trying to do complex configuration in eunit, but with no effect.

I want to output some details of the exception to the eunit test result.

Thanks ~

+7
source share
4 answers

Eunit is quite old, and although it is officially supported by the OTP team in Ericsson, this is not usually provided. Eunit currently has a bad habit of wasting stack traces and has not been updated for R15 line numbers in exceptions.

I would not argue about how it should work. No sensible test tool should hide exception data and line numbers for you.

+4
source

The problem is that the eunit version shipped with R15 does not understand the new stack trace format in R15. This has been fixed in the eunit developer version: github.com/richcarl/eunit

For example:

 Eshell V5.10 (abort with ^G) 1> eunit:test(fun() -> (fun() -> exit(foo), ok end)() end). erl_eval: expr...*failed* in function erl_eval:do_apply/6 (erl_eval.erl, line 576) in call from erl_eval:exprs/5 (erl_eval.erl, line 118) **exit:foo 

Hope this moves it to the next version of OTP R15.

+7
source

This is a known issue in eunit released in R15B and R15B01. This has been fixed in the R15B02 release. If you stick to an earlier version, you can download and apply the patch :

Workaround for releases prior to R15B02

You can fix the problem in your local installation by recompiling the affected module:

  • Download and unzip Erlang / OTP sources if you don’t already have them.

     wget http://www.erlang.org/download/otp_src_R15B01.tar.gz tar xzf otp_src_R15B01.tar.gz cd otp_src_R15B01 
  • Download and apply the patch .

     wget -O eunit-stacktrace.patch https://github.com/erlang/otp/commit/73b94a990bb91fd263dace4ccbaef6ff727a9637.patch patch -p1 < eunit-stacktrace.patch 
  • Recompile eunit_lib.erl .

     cd lib/eunit erlc -o ebin -I include src/eunit_lib.erl 
  • Copy the new eunit_lib.beam on top of the old one (usually somewhere below /usr/local ).

     ls /usr/local/lib/erlang/lib/eunit-2.2.2/ebin/ # check that eunit_lib.beam is actually there sudo cp ebin/eunit_lib.beam /usr/local/lib/erlang/lib/eunit-2.2.2/ebin/ 
+7
source

I like to use the ?debugVal(catch expr) trick, where expr is either a begin end block or a call to a failure function. For example ?debugVal(catch begin 1 = 2 end) will output stacktrace in your tests.

+2
source

All Articles