【问题标题】:run eunit test from console using erl -noshell使用 erl -noshell 从控制台运行 eunit 测试
【发布时间】:2011-07-02 05:19:30
【问题描述】:

我想从控制台运行以下 eunit 测试命令

eunit:test([test_module, [verbose]).

我试过了,但似乎不起作用 erl -noshell -pa ./ebin -s eunit test test_module verbose -init stop

~/uid_server$erl -noshell -pa ./ebin -s eunit test test_module verbose -init stop
undefined
*** test module not found ***
::test_module

=======================================================
  Failed: 0.  Skipped: 0.  Passed: 0.
One or more tests were cancelled.

你知道如何从控制台正确传递不简单的参数吗?

【问题讨论】:

    标签: erlang


    【解决方案1】:

    您可以尝试引用参数而不是列表。 erl -noshell -pa ./ebin -s eunit test "test_module verbose" -init stop

    【讨论】:

      【解决方案2】:

      您的参数看起来不对。这应该有效:

      erl -noshell -pa ebin -eval "eunit:test(test_module, [verbose])" -s init stop
      

      -s只能通过指定模块和函数名来运行不带参数的函数(例如initstop执行init:stop())。

      您也可以像这样将一个列表传递给元数 1 的函数:

      -s foo bar a b c
      

      会打电话

      foo:bar([a,b,c])
      

      所有参数仅作为原子列表传递(即使您尝试使用其他字符,例如数字,它们也会转换为原子)。

      因此,如果要运行eunit:test/2,则要传递两个参数而不仅仅是原子,因此必须使用-eval,它将包含Erlang代码的字符串作为参数。所有-eval-s 函数都按照它们定义的顺序依次执行。

      另外,请确保您的测试代码也在 ./ebin 中(否则请写 -pa ebin test_ebin,其中 test_ebin 是您的测试代码所在的位置)。

      【讨论】:

        【解决方案3】:

        你也可以使用钢筋...

        • 通过 cd'ing 到您的项目目录并键入以下内容来获取 rebar:

          curl @987654321@ -o rebar

          chmod u+x rebar

        • 将以下内容添加到您正在测试的模块中,就在上次导出之后:

          -ifdef(TEST).

          -include_lib("eunit/include/eunit.hrl").

          -endif.

        • 接下来,在模块底部添加测试,用 ifdef 包裹,如下所示:

          -ifdef(TEST).

          simple_test() ->

              ?assertNot(true).

          -endif.

        • 最后,像这样从你的 shell 运行 rebar:

          ./rebar compile eunit

        【讨论】:

          【解决方案4】:

          我使用这个脚本:https://github.com/lafka/dotconfig/blob/master/bin/eunit-module 在特定模块上运行 eunit。

          示例:

          eunit-module <module> src ebin -I deps   
          

          这将做几件事:

          • Arg #2 是 .erl 所在的目录
          • Arg #3 是输出编译后的.beam 的目录
          • Arg #4++ 是要添加到代码路径的所有附加路径
          • 使用 -I 指定额外的代码路径以及查找使用 -include_lib 引用的文件的位置

          【讨论】:

            【解决方案5】:

            这个问题已经过去八年多了,但仍然有一个很好的解决方案在之前的答案中没有提到。

            一旦您使用了 EUnit,您就可以利用它的一些“自动”功能。其中之一是自动导出 test/0 函数,其中包含模块的所有测试。

            因此,如果您将测试与源代码一起编写在同一个模块中,您所要做的就是:

            $ erl -noshell -run your_module test -run init stop
            

            如果你在一个独立的、依赖的模块中编写测试(你应该这样做),你必须指向那个模块:

            $ erl -noshell -run your_module_tests test -run init stop
            

            所有这些都可以正常工作,但测试不会按照 OP 的要求在详细模式下运行,但这很容易通过将 EUNIT 环境变量设置为 verbose 来解决。

            最终版本:

            $ EUNIT=verbose erl -noshell -run your_module_tests test -run init stop
            

            享受 Erlang 和 EUnit 的乐趣!

            【讨论】:

              猜你喜欢
              • 2021-05-15
              • 1970-01-01
              • 2010-10-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多