使用erl -noshell从控制台运行eunit测试

时间:2011-02-22 18:31:51

标签: erlang

我想从控制台

运行以下eunit test命令
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.

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

5 个答案:

答案 0 :(得分:14)

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

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

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

您还可以将一个列表传递给arity 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是您的测试代码所在的位置。

答案 1 :(得分:2)

您也可以使用螺纹钢......

  • 通过cd到项目目录获取钢筋并输入以下内容:

    curl http://cloud.github.com/downloads/basho/rebar/rebar -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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我使用这个脚本: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
  • 引用的文件的位置

答案 4 :(得分:0)

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

一旦使用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一起玩吧!