简体   繁体   中英

How to setup tests with the appropriate code using rebar3?

I have created a simple application via rebar3 templates such as:

apps/myapp/app/myapp_app.erl

-module(myapp_app).

-behaviour(application).

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

start(_StartType, _Params) ->
    ok.

stop(_State) ->
    ok.

I have written a test for that:

apps/myapp/test/myapp_test.erl

-module(myapp_test).

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

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

Sadly, when I launch the test, it seems that the link is not done between the two files:

$ 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

Is there something to add in rebar.config ?

If you have .erl files in a custom directory other than 'src', then you need to add it to the code path.

You can do it using rebar3 by modifying erl_opts section in 'rebar.config' as below.

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

Hope this can work for you.

rebar3 does not find your .erl files if you have been under /app I moved them to /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 means could not be found when evaluating the call at runtime. 2 To help see what rebar3 is doing I can highly recommend debug , DEBUG=1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM