如何使用 rebar3 使用适当的代码设置测试?

How to setup tests with the appropriate code using rebar3?

我已经通过 rebar3 模板创建了一个简单的应用程序,例如:

apps/myapp/app/myapp_app.erl

-module(myapp_app).

-behaviour(application).

-export([start/2, stop/1]).

start(_StartType, _Params) ->
    ok.

stop(_State) ->
    ok.

我已经为此编写了一个测试:

apps/myapp/test/myapp_test.erl

-module(myapp_test).

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

simple_test() ->
    myapp_app:start(ok, 42).

遗憾的是,当我启动测试时,似乎 link 没有在两个文件之间完成:

$ rebar3 eunit
===> Verifying dependencies...
===> Compiling shoreline
===> Performing EUnit tests...
F
Failures:

  1) myapp_test:simple_test/0
     Failure/Error: {error,undef,
                        [{myapp_app,start,"*",[]},
                         {myapp_test,simple_test,0,
                             [{file,
                                  "/.../apps/myapp/test/myapp_test.erl"},
                              {line,8}]},
                         {myapp_test,simple_test,0,[]}]}
     Output:

Finished in 0.074 seconds
1 tests, 1 failures
===> Error running tests

rebar.config有什么要补充的吗?

rebar3 没有找到你的 .erl 文件如果你已经在 /app 我把它们移到了 /src

➜  myapp rebar3 eunit
===> Verifying dependencies...
===> Compiling myapp
===> Performing EUnit tests...
.
Finished in 0.081 seconds
1 tests, 0 failures
➜  myapp ls
LICENSE      README.md    _build       rebar.config src          test
➜  myapp mv src app
➜  myapp rebar3 eunit
===> Verifying dependencies...
===> Performing EUnit tests...
F
Failures:

  1) myapp_test:simple_test/0: module 'myapp_test'
     Failure/Error: {error,undef,
                           [{myapp_app,start,[ok,42],[]},
                            {myapp_test,simple_test,0,[]}]}
     Output:

Finished in 0.036 seconds
1 tests, 1 failures
===> Error running tests

undef 表示可以 not be found when evaluating the call at runtime. 2 To help see what rebar3 is doing I can highly recommend debug, DEBUG=1.

如果您在 'src' 以外的自定义目录中有 .erl 文件,则需要将其添加到代码路径中。

您可以使用 rebar3 通过修改 'rebar.config' 中的 erl_opts 部分来完成,如下所示。

{erl_opts, [debug_info, {src_dirs, ["src", "app"]}]}. 

希望这对你有用。