【问题标题】:Getting test results from Eunit in Erlang在 Erlang 中从 Eunit 获取测试结果
【发布时间】:2012-06-17 03:31:38
【问题描述】:

我正在使用 Erlang 和 EUnit 进行单元测试,我想编写一个测试运行程序来自动运行我的单元测试。问题是 eunit:test/1 似乎只返回“错误”或“确定”,而不是测试列表以及它们根据通过或失败返回的内容。

那么有没有办法运行测试并取回某种形式的测试运行的数据结构及其通过/失败状态?

【问题讨论】:

    标签: erlang eunit


    【解决方案1】:

    我刚刚向 GitHub 推送了一个非常简单的侦听器,它将 EUnit 结果存储在 DETS 表中。如果您需要进一步处理这些数据,这可能很有用,因为它们作为 Erlang 术语存储在 DETS 表中。

    https://github.com/prof3ta/eunit_terms

    使用示例:

    > 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,<<>>}]}]
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      如果您使用钢筋,则不必实现自己的跑步者。你可以简单地运行:

      rebar eunit
      

      Rebar 将编译并运行 test 目录中的所有测试(以及模块内的 eunit 测试)。此外,rebar 允许您在 rebar.config 中设置与 shell 中相同的选项:

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

      您也可以在 shell 中使用这些选项:

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

      另请参阅 verbose optionstructured report 的文档。

      另一种选择是使用 Common Test 而不是 Eunit。 Common Test 带有一个运行器(ct_run 命令),它为您的测试设置提供了更大的灵活性,但使用起来也有点复杂。 Common Test 缺少可用的宏,但会生成非常易于理解的 html 报告。

      【讨论】:

        【解决方案3】:

        没有简单的或记录在案的方法,但目前有两种方法可以做到这一点。一种是在运行测试时提供“event_log”选项:

        eunit:test(my_module, [event_log])
        

        (这是未记录的,实际上仅用于调试)。生成的文件“eunit-events.log”是一个文本文件,Erlang 可以使用 file:consult(Filename) 读取它。

        更强大的方法(并不是那么困难)是实现一个自定义事件监听器并将其作为一个选项提供给 eunit:

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

        这还没有记录,但它应该是。监听器模块实现了 eunit_listener 行为(参见 src/eunit_listener.erl)。只有五个回调函数需要实现。查看 src/eunit_tty.erl 和 src/eunit_surefire.erl 中的示例。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-29
          • 2012-10-29
          相关资源
          最近更新 更多