how to use rebar to create an erlang module with an eunit test?

我与影子孤独终老i 提交于 2019-12-25 09:42:38

问题


My goal is quite simple; while I am learning Erlang, I would like to use rebar to create a basic module with an eunit test:

I have tried the following:

mkdir erlangscratch
cd erlangscratch
rebar create template=simplemod modid=erlangscratch

Edit the 'test/erlangscratch_tests.erl' to look like this:

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

% This should fail
basic_test_() ->
    ?assert(1 =:= 2).

Execute the tests:

snowch@tp:~/erlangscratch$ rebar co eu
==> erlangscratch (compile)
==> erlangscratch (eunit)

The tests weren't executed, but it also seems that the code isn't compiling.

Here are the contents of my folder:

snowch@tp:~/erlangscratch$ tree .
.
├── src
│   └── erlangscratch.erl
└── test
    └── erlangscratch_tests.erl

2 directories, 2 files

Question: What step(s) have I missed out?


UPDATE:

As per the accepted answer, basic_test_ function needed to be renamed and the 'src/erlangscratch.app.src' was missing so I created it with the following contents:

{application, erlangscratch,
 [
  {description, "An Erlang erlangscratch library"},
  {vsn, "1"},
  {modules, [
             erlangscratch
            ]},
  {registered, []},
  {applications, [
                  kernel,
                  stdlib
                 ]},
  {env, []}
 ]}.

回答1:


You are mixing tests with test generators.

In short the second one should return fun's or lists of fun's. You can distinguish both by _ at the end of your tests names, and _ and the beginning of your macros.

Simple solution would be using either

basic_test() ->
    ?assert(1 =:= 2).

or

basic_test_() ->
    ?_assert(1 =:= 2).

depending on your needs, and what you understand better.


EDIT after sharing folder structure

It seems rebar is not recognizing your project as an OTP application. You might be just missing simple .app.src file. Something like:

{application, myapp,
 [
  {description, ""},
  {vsn, "1"},
  {registered, []},
  {applications, [
                  kernel,
                  stdlib
                 ]},
  {env, []}
 ]}.

And since rebar is capable of generating one for you, you either just call rebar create-app or extend one of existing templates.



来源:https://stackoverflow.com/questions/27100804/how-to-use-rebar-to-create-an-erlang-module-with-an-eunit-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!