【问题标题】:A common setup and teardown method for Erlang Eunit test suitesErlang Eunit 测试套件的常用设置和拆卸方法
【发布时间】:2017-02-23 05:00:22
【问题描述】:

我正在尝试检查我在 MongoDB 中定义的所有索引是否都被我的应用程序使用,并且没有额外的索引。我有一个实用程序可以为单个 Eunit 测试套件执行此操作。但是,我的一些组件有多个Eunit 测试套件,我想知道是否有办法在调用任何测试套件之前运行通用代码,然后在所有测试套件完成后运行通用拆卸代码.我正在使用rebar 来调用Eunit

提前致谢。

【问题讨论】:

    标签: erlang eunit


    【解决方案1】:

    您可以查看灯具,尤其是 setup 一个:http://erlang.org/doc/apps/eunit/chapter.html#Fixtures

    【讨论】:

      【解决方案2】:

      只要eunit documentation for test representations 解释,测试集可以是深度列表。下面是一个示例模块,展示了使用外部setup 夹具,其测试是生成器,每个都提供一个内部setup 夹具。内部的setup 固定装置对应于您现有的测试套件,每个测试套件都有自己的设置和清理功能。外部的setup 固定装置为内部套件提供了通用设置和清理。

      -module(t).
      -compile(export_all).
      
      -include_lib("eunit/include/eunit.hrl").
      
      top_setup() ->
          ?debugMsg("top setup").
      
      top_cleanup(_) ->
          ?debugMsg("top cleanup").
      
      test_t1() ->
          {setup,
           fun() -> ?debugMsg("t1 setup") end,
           fun(_) -> ?debugMsg("t1 cleanup") end,
           [fun() -> ?debugMsg("t1 test 1") end,
            fun() -> ?debugMsg("t1 test 2") end,
            fun() -> ?debugMsg("t1 test 3") end]}.
      
      test_t2() ->
          {setup,
           fun() -> ?debugMsg("t2 setup") end,
           fun(_) -> ?debugMsg("t2 cleanup") end,
           [fun() -> ?debugMsg("t2 test 1") end,
            fun() -> ?debugMsg("t2 test 2") end,
            fun() -> ?debugMsg("t2 test 3") end]}.
      
      t_test_() ->
          {setup,
           fun top_setup/0,
           fun top_cleanup/1,
           [{generator, fun test_t1/0},
            {generator, fun test_t2/0}]}.
      

      编译这个模块,然后从 Erlang shell 运行它会产生预期的输出:

      1> c(t).
      {ok,t}
      2> eunit:test(t).
      /tmp/t.erl:7:<0.291.0>: top setup
      /tmp/t.erl:14:<0.293.0>: t1 setup
      /tmp/t.erl:16:<0.295.0>: t1 test 1
      /tmp/t.erl:17:<0.295.0>: t1 test 2
      /tmp/t.erl:18:<0.295.0>: t1 test 3
      /tmp/t.erl:15:<0.293.0>: t1 cleanup
      /tmp/t.erl:22:<0.293.0>: t2 setup
      /tmp/t.erl:24:<0.300.0>: t2 test 1
      /tmp/t.erl:25:<0.300.0>: t2 test 2
      /tmp/t.erl:26:<0.300.0>: t2 test 3
      /tmp/t.erl:23:<0.293.0>: t2 cleanup
      /tmp/t.erl:10:<0.291.0>: top cleanup
        All 6 tests passed.
      ok
      

      通用设置首先运行,然后每个套件由其自己的设置和清理运行,最后运行通用清理。

      【讨论】:

      • 您的代码示例是探索eunit 文档的良好起点。非常有用。
      猜你喜欢
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      相关资源
      最近更新 更多