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

发布于 2024-10-18 20:19:18 字数 506 浏览 2 评论 0原文

我想从控制台运行以下 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.

您知道如何从控制台正确传递非简单参数吗?

I wanted to run the following eunit test command from console

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

I tried this, but seems not working
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.

Do you know how to pass not a simple arguments properly from console?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉栀 2024-10-25 20:19:18

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

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 是您的测试代码所在的位置)。

Your parameters look wrong. This should work:

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

-s can only run functions without arguments by specifying the module and function name (for example init and stop to execute init:stop()).

You can also pass one list to a function of arity 1 like this:

-s foo bar a b c

would call

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

All the parameters are passed as a list of atoms only (even when you try to use some other characters, such as numbers, they are converted to atoms).

So since you want to pass two params and not only atoms if you want to run eunit:test/2 you'd have to use -eval which takes a string containing Erlang code as an argument. All -eval and -s functions are executed sequentially in the order they are defined.

Also, make sure you have your test code in ./ebin as well (otherwise write -pa ebin test_ebin where test_ebin is where your test code is).

久隐师 2024-10-25 20:19:18

您还可以使用 rebar...

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

    curl http://cloud.github.com/downloads/basho/钢筋/钢筋 -o 钢筋

    chmod u+x rebar

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

    -ifdef(测试)。

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

    -endif.

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

    -ifdef(测试)。

    simple_test() ->

        ?assertNot(true)。

    -endif.

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

    ./rebarcompileeunit

You can also use rebar...

  • Get rebar by cd'ing to your project directory and typing the following:

    curl http://cloud.github.com/downloads/basho/rebar/rebar -o rebar

    chmod u+x rebar

  • Add the following to your module under test, right after last export:

    -ifdef(TEST).

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

    -endif.

  • Next, add your tests at the bottom of your module, wrapped in an ifdef like so:

    -ifdef(TEST).

    simple_test() ->

        ?assertNot(true).

    -endif.

  • Lastly, run rebar from your shell like so:

    ./rebar compile eunit

放赐 2024-10-25 20:19:18

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

you can try quote parameters instead of listing.
erl -noshell -pa ./ebin -s eunit test "test_module verbose" -init stop

暗喜 2024-10-25 20:19:18

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

一旦您使用 EUnit,您就可以利用它的一些“automagic”功能。其中之一是自动导出 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 的乐趣!

More than eight years passed since the question, but there's still a nice solution not mentioned in the previous answers.

Once you are using EUnit, you can leverage from some of it's "automagic" features. One of them is an automatic export of the test/0 function, containing all the tests for the module.

So, if you are writing your tests alongside with the source code in the same module, all you have to do is:

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

If you are writing the tests in a separated, dependent module (as you should), you have to point to that module:

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

All this will work fine, but the test won't be run in verbose mode as the OP required, but this is simple to solve with the EUNIT environment variable set to verbose.

Final version:

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

Have fun with Erlang and EUnit!

小…楫夜泊 2024-10-25 20:19:18

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

I use this script: https://github.com/lafka/dotconfig/blob/master/bin/eunit-module to run eunit on specific modules.

Example:

eunit-module <module> src ebin -I deps   

That will do a couple of things:

  • Arg #2 is the directory where .erl resides
  • Arg #3 is the directory to output the compiled .beam
  • Arg #4++ is all the additional paths to add to your code path
  • Using -I specifies additional code paths AND where to look for files referenced with -include_lib
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文