在erlang eunit故障中打印测试夹具描述

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在erlang eunit故障中打印测试夹具描述相关的知识,希望对你有一定的参考价值。

有没有办法打印使用灯具的erlang测试生成器的测试描述?使用生成器使得告诉哪些测试实际失败并且打印描述会有所帮助变得棘手。

例:

-module(math_test).

-include_lib("eunit/include/eunit.hrl").
-define(test(Desc, F), {Desc, {setup, fun setup/0, fun cleanup/1, F}}).

setup() ->
  ok.

cleanup(_) ->
  ok.

math_test_ () ->
  [
    ?test("adds two numbers", fun add_two_numbers/0),
    ?test("subtract two numbers", fun subtract_two_numbers/0),
    ?test("undefined method called", fun undefined_error/0)
  ].

add_two_numbers () ->
  ?assertEqual(2, 1 + 3).

subtract_two_numbers () ->
  ?assertEqual(1, 2 - 2).

undefined_error () ->
  undefined_module:uh_oh().

然后运行它

[root@a7c901c022bb src]# rebar3 eunit --module=math_test
===> Verifying dependencies...
===> Compiling math
===> Performing EUnit tests...
FFF
Failures:

  1) math_test:math_test_/0
     Failure/Error: ?assertEqual(2, 1 + 3)
       expected: 2
            got: 4
     %% /src/_build/test/lib/math/src/math_test.erl:20:in `math_test:-add_two_numbers/0-fun-0-/1`
     Output: 
     Output: 
  2) math_test:math_test_/0
     Failure/Error: ?assertEqual(1, 2 - 2)
       expected: 1
            got: 0
     %% /src/_build/test/lib/math/src/math_test.erl:23:in `math_test:-subtract_two_numbers/0-fun-0-/1`
     Output: 
     Output: 
  3) math_test:math_test_/0
     Failure/Error: {error,undef,[{undefined_module,uh_oh,[],[]}]}
     Output: 

前两个错误是可以的,但不是很好 - 你至少可以在断言中看到实际出错的地方。

然而,第三个错误(调用未定义的模块/方法)是可怕的错误 - 没有真正的方法来告诉它来自哪里!

有没有办法改进,比如用故障日志打印测试描述?

答案

你可以做的一件事是将测试描述放在测试本身,而不是整个setup元组。也就是说,改变这一行:

-define(test(Desc, F), {Desc, {setup, fun setup/0, fun cleanup/1, F}}).

至:

-define(test(Desc, F), {setup, fun setup/0, fun cleanup/1, {Desc, F}}).

通过该更改,将打印测试说明:

Failures:

  1) math_test:math_test_/0: adds two numbers
     Failure/Error: ?assertEqual(2, 1 + 3)
       expected: 2
            got: 4
     %% /tmp/math_test/mylib/_build/test/lib/mylib/test/math_test.erl:20:in `math_test:-add_two_numbers/0-fun-0-/0`
     Output: 
     Output: 
  2) math_test:math_test_/0: subtract two numbers
     Failure/Error: ?assertEqual(1, 2 - 2)
       expected: 1
            got: 0
     %% /tmp/math_test/mylib/_build/test/lib/mylib/test/math_test.erl:23:in `math_test:-subtract_two_numbers/0-fun-0-/0`
     Output: 
     Output: 
  3) math_test:math_test_/0: undefined method called
     Failure/Error: {error,undef,[{undefined_module,uh_oh,[],[]}]}
     Output: 

另一件事是使用?_test宏而不是简单的fun术语来指定测试函数:

math_test_ () ->
  [
    ?test("adds two numbers", ?_test(add_two_numbers())),
    ?test("subtract two numbers", ?_test(subtract_two_numbers())),
    ?test("undefined method called", ?_test(undefined_error()))
  ].

?_test宏会记住它出现的行号,并在测试失败时将其包含在输出中:

  1) math_test:math_test_/0:14: adds two numbers
  [...]
  2) math_test:math_test_/0:15: subtract two numbers
  [...]
  3) math_test:math_test_/0:16: undefined method called
  [...]

现在,您可以确定从哪个行调用这些测试。


另一种方法是让各个函数返回eunit“测试对象”而不是仅运行测试。这将涉及使用?_assertEqual而不是?assertEqual,或包装整个事情在?_test

math_test_ () ->
  [
    ?test("adds two numbers", add_two_numbers()),
    ?test("subtract two numbers", subtract_two_numbers()),
    ?test("undefined method called", undefined_error())
  ].

add_two_numbers () ->
  ?_assertEqual(2, 1 + 3).

subtract_two_numbers () ->
  ?_assertEqual(1, 2 - 2).

undefined_error () ->
  ?_test(undefined_module:uh_oh())

然后输出包含行号和各个测试函数的名称:

Failures:

  1) math_test:add_two_numbers/0:20: adds two numbers
     Failure/Error: ?assertEqual(2, 1 + 3)
       expected: 2
            got: 4
     %% /tmp/math_test/mylib/_build/test/lib/mylib/test/math_test.erl:20:in `math_test:-add_two_numbers/0-fun-0-/0`
     Output: 
     Output: 
  2) math_test:subtract_two_numbers/0:23: subtract two numbers
     Failure/Error: ?assertEqual(1, 2 - 2)
       expected: 1
            got: 0
     %% /tmp/math_test/mylib/_build/test/lib/mylib/test/math_test.erl:23:in `math_test:-subtract_two_numbers/0-fun-0-/0`
     Output: 
     Output: 
  3) math_test:undefined_error/0:26: undefined method called
     Failure/Error: {error,undef,[{undefined_module,uh_oh,[],[]}]}
     Output: 
另一答案

@legoscia的答案很好,但我也怀疑这是由rebar3实现的错误报告对于那种错误而言不是最理想的。使用默认输出直接从eunit运行测试,你会得到:

2> eunit:test(math_test). 
math_test: math_test_...*failed*
in function math_test:'-add_two_numbers/0-fun-0-'/0 (math_test.erl, line 22)
**error:{assertEqual,[{module,math_test},
                      {line,22},
                      {expression,"1 + 3"},
                      {expected,2},
                      {value,4}]}
  output:<<"">>

math_test: math_test_...*failed*
in function math_test:'-subtract_two_numbers/0-fun-0-'/0 (math_test.erl, line 25)
**error:{assertEqual,[{module,math_test},
                      {line,25},
                      {expression,"2 - 2"},
                      {expected,1},
                      {value,0}]}
  output:<<"">>

math_test: math_test_...*failed*
in function undefined_module:uh_oh/0
  called as uh_oh()
**error:undef 
  output:<<"">>

=======================================================
  Failed: 3.  Skipped: 0.  Passed: 0.

使用'verbose'选项,它还会在每次设置之前打印描述。此外,将描述移动到测试乐趣,并使用?_test(...)宏来创建具有更多位置信息的测试乐趣而不是简单的乐趣,正如@legoscia建议的那样,您将获得此输出:

  math_test:18: math_test_ (undefined method called)...*failed*
in function undefined_module:uh_oh/0
  called as uh_oh()
**error:undef 
  output:<<"">>

你可以向rebar3维护者报告。

以上是关于在erlang eunit故障中打印测试夹具描述的主要内容,如果未能解决你的问题,请参考以下文章

如何在所有 eunit 案例之前启动应用程序

为啥 Eunit 坚持我的函数返回 ok, value 而不是?

eunit 测试超时

NUnit 有时以“无法定位夹具”结尾

如何在 symfony WebTestCase 的测试中按夹具类型获取教义夹具参考?

标记一个 Pytest 夹具而不是使用该夹具的所有测试