使用 erl -noshell 从控制台运行 eunit 测试
我想从控制台运行以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的参数看起来不对。这应该有效:
-s
只能通过指定模块和函数名称来运行不带参数的函数(例如init
和stop
来执行init:stop()
)。您还可以将一个列表传递给 arity 1 的函数,如下所示:
将调用
所有参数仅作为原子列表传递(即使您尝试使用其他一些字符,例如数字,它们也会转换为原子)。
因此,既然你想传递两个参数,而不仅仅是原子,如果你想运行
eunit:test/2
,你必须使用-eval
,它接受一个包含 Erlang 的字符串代码作为参数。所有-eval
和-s
函数都按照定义的顺序依次执行。另外,请确保您的测试代码也在 ./ebin 中(否则编写
-pa ebin test_ebin
,其中test_ebin
是您的测试代码所在的位置)。Your parameters look wrong. This should work:
-s
can only run functions without arguments by specifying the module and function name (for exampleinit
andstop
to executeinit:stop()
).You can also pass one list to a function of arity 1 like this:
would call
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
wheretest_ebin
is where your test code is).您还可以使用 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
您可以尝试引用参数而不是列出。
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
问题提出已经八年多了,但仍然有一个很好的解决方案,在之前的答案中没有提到。
一旦您使用 EUnit,您就可以利用它的一些“automagic”功能。其中之一是自动导出
test/0
函数,其中包含模块的所有测试。因此,如果您在同一模块中与源代码一起编写测试,那么您所要做的就是:
如果您在单独的依赖模块中编写测试(正如您应该的那样),则必须指向该模块:
所有这些都可以正常工作,但测试不会按照 OP 的要求在详细模式下运行,但这很容易通过将
EUNIT
环境变量设置为verbose
来解决>。最终版本:
享受 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:
If you are writing the tests in a separated, dependent module (as you should), you have to point to that module:
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 toverbose
.Final version:
Have fun with Erlang and EUnit!
我使用这个脚本: https://github.com/lafka/dotconfig/ blob/master/bin/eunit-module 在特定模块上运行 eunit。
示例:
这将执行以下操作:
I use this script: https://github.com/lafka/dotconfig/blob/master/bin/eunit-module to run eunit on specific modules.
Example:
That will do a couple of things: