当前位置: 动力学知识库 > 问答 > 编程问答 >

erlang - not output exception stack trace in EUnit

问题描述:

I'm write a test with EUnit, but not anything exception detail output in console.

exp_test() ->

?assertEqual(0, 1/0).

Run this module:exp_test() in the Erlang Shell output following

** 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 following

> eunit:test(xxx).

> xxx_test: exp_test...*failed*

::badarith

EUnit not output anything exception trace info

Im trying the verbose config in eunit, but no effect.

I want to output some exception detail in eunit test result.

Thanks~

网友答案:

Eunit is quite old and while it is officially maintained by the OTP team at Ericsson, it is usually uncared for. Eunit currently has the bad habit of eating up stack traces, and hasn't been updated for R15's line numbers in exceptions.

I wouldn't argue that "that's how it's supposed to work". No sane test tool should hide exception details and line numbers for you.

网友答案:

The problem seems to be that the version of eunit shipped with R15 does not understand the new stack trace format in R15. This has been fixed in the development version of eunit: 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

I hope this will make it into the next release of OTP R15.

网友答案:

This is a known problem in eunit as released in R15B and R15B01. This has been fixed in release R15B02. If you're stuck with an earlier version, you can download and apply a patch:

A workaround for releases before R15B02

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

  1. Download and unpack the Erlang/OTP sources, if you don't have them already.

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

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

    cd lib/eunit
    erlc -o ebin -I include src/eunit_lib.erl
    
  4. Copy the new eunit_lib.beam over 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/
    
网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: