Erlang build tools-Series 1

Source: Internet
Author: User
Getting startedfirst steps

The best way to get going with rebar is to use it to construct and
Build a simple Erlang application. First, let's create the directory
Our application:

 
$ Mkdir MyApp; CD MyApp

Now, download the rebar binary into our application. Note: If you
Have rebar available on your shell path, that will work equally well.

 
$ Wget http://bitbucket.org/basho/rebar/downloads/rebar; chmod U + x rebar

Now, we'll use rebar's templating system to create the skeleton
Our application:

 
$./Rebar create-app appid = MyApp

The result of this command will be a single sub-directory, "src"
Which contains three files:

    • MyApp. App. Src-the OTP application specification
    • Myapp_app.erl-an implementation of the OTP Application
      Behaviour
    • Myapp_sup.erl-an implementation of an OTP Supervisor
      Behaviour

We can now compile this application with rebar:

 
$./Rebar compile

A new directory, "Ebin/" will now exist and in it you will find
. Beam files
Corresponding to the Erlang source files in the source directory. In
Addition,
Note the presence of Ebin/MyApp. app -- rebar has dynamically generated
Proper
OTP application specification using the template in src/MyApp. App. SRC.

To cleanup the directory after compiling simple do:

 
$./Rebar clean
Testing

Rebar provides support for both eunit and common_test Testing
Frameworks. In this example, we'll use eunit and write a simple unit
Test for our application.

In src/myapp_app.erl, after the-Export () Directive, add
Following code:

 
-Ifdef (test ).
-Include_lib ("eunit/include/eunit. HRL ").
-Endif.

In addition, at the end of the same file, add this Code:

 
-Ifdef (test ).

Simple_test ()->
OK = Application: Start (MyApp ),
? Assertnot (undefined = whereis (myapp_sup )).

-Endif.

By wrapping the test code with that ifdef directive, we can be sure
Our test code won't be shipped as part of the compiled code in Ebin.
Let's run the unit test now:

 
$./Rebar compile eunit

You shoshould see output something like:

==> MyApp (compile)
Compiled src/myapp_app.erl
==> MyApp (eunit)
Compiled src/myapp_app.erl
Test passed

Notice that rebar compiled myapp_app.erl twice; the second
Compilation output to a special directory (. eunit) and merge des debug
Information and other helpful testing flags.

If we want to check the coverage of our unit tests, we can easily do
That by adding the following line to a rebar. config file:

 
{Cover_enabled, true }.

Now re-run our tests:

 
$./Rebar compile eunit
==> MyApp (compile)
==> MyApp (eunit)
Test passed.
Cover analysis:/users/dizzyd/tmp/MyApp/. eunit/index.html
$

You can look in the. eunit/index.html file for details of
Coverage analysis.

 

Reprinted: https://bitbucket.org/basho/rebar/wiki/GettingStarted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.