Search code examples
erlangcowboycommon-test

Testing Erlang applications based on cowboy using common test


I have an Erlang application based on Cowboy and I would like to test it.

Previously I used wooga's library etest_http for this kind of tasks, but I would like to start using common tests since I noticed that this is the way used in cowboy. I have tried to setup a very basic test but I am not able to run it properly.

Can anybody provide me a sample for testing the basic example echo_get and tell me what is the correct way to run the test from the console using the Makefile contained in the example?


Solution

  • The example make used only for build echo_get app. So to test echo_get app you can add test suite and call make && rebar -v 1 skip_deps=true ct (rebar should be in PATH) from shell. Also you need etest and etest_http in your Erlang PATH or add it with rebar.config in your application. You can use httpc or curl with os:cmd/1 instead ehttp_test :)

    test/my_test_SUITE.erl (full example)

    -module(my_test_SUITE).
    
    -compile(export_all).
    
    -include_lib("common_test/include/ct.hrl").
    
    % etest macros
    -include_lib ("etest/include/etest.hrl").
    % etest_http macros
    -include_lib ("etest_http/include/etest_http.hrl").
    
    suite() ->
        [{timetrap,{seconds,30}}].
    
    init_per_suite(Config) ->
        %% start your app or release here
        %% for example start echo_get release
        os:cmd("./_rel/bin/echo_get_example start"),
        Config.
    
    end_per_suite(_Config) ->
        %% stop your app or release here
        %% for example stop echo_get release
        os:cmd("./_rel/bin/echo_get_example stop")
        ok.
    
    init_per_testcase(_TestCase, Config) ->
        Config.
    
    end_per_testcase(_TestCase, _Config) ->
        ok.
    
    all() -> 
        [my_test_case].
    
    my_test_case(_Config) ->
        Response = ?perform_get("http://localhost:8080/?echo=saymyname"),
        ?assert_status(200, Response),
        ?assert_body("saymyname", Response).
        ok.