3

我有一个基于 Cowboy 的 Erlang 应用程序,我想对其进行测试。

以前我使用 wooga 的库etest_http来完成此类任务,但我想开始使用常见测试,因为我注意到这是牛仔中使用的方式。我试图设置一个非常基本的测试,但我无法正确运行它。

任何人都可以为我提供一个测试基本示例echo_get的示例,并告诉我使用示例中包含的 Makefile 从控制台运行测试的正确方法是什么?

4

3 回答 3

2

该示例仅用于构建 echo_get 应用程序。因此,要测试 echo_get 应用程序,您可以添加测试套件并make && rebar -v 1 skip_deps=true ct从 shell 调用(rebar 应该在 PATH 中)。您还需要在 Erlang PATH 中使用 etest 和 etest_http 或在应用程序中使用 rebar.config 添加它。您可以将 httpc 或 curl 与 os:cmd/1 一起使用,而不是 ehttp_test :)

test/my_test_SUITE.erl(完整示例

-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.
于 2013-11-05T05:27:46.783 回答
1

以下启动 hello_world 应用程序及其依赖项,但使用最近编译的版本,而不是 ./_rel 中的版本;这可能是也可能不是你想要的,但它确实避免了timer:sleep(1000)

-module(hello_world_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([all/0, init_per_suite/1, end_per_suite/1]).
-export([http_get_hello_world/1]).

all() ->
    [http_get_hello_world].

init_per_suite(Config) ->
    {ok, App_Start_List} = start([hello_world]),
    inets:start(),
    [{app_start_list, App_Start_List}|Config].

end_per_suite(Config) ->
    inets:stop(),
    stop(?config(app_start_list, Config)),
    Config.

http_get_hello_world(_Config) ->
    {ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} =
        httpc:request(get, {"http://localhost:8080", []}, [], []),
    Body = "Hello World!\n".

start(Apps) ->
    {ok, do_start(_To_start = Apps, _Started = [])}.

do_start([], Started) ->
    Started;
do_start([App|Apps], Started) ->
    case application:start(App) of
    ok ->
        do_start(Apps, [App|Started]);
    {error, {not_started, Dep}} ->
        do_start([Dep|[App|Apps]], Started)
    end.

stop(Apps) ->
    _ = [ application:stop(App) || App <- Apps ],
    ok.
于 2014-07-10T15:02:51.237 回答
0

使用https://github.com/extend/gun

运行提供的示例(考虑“用户”文件夹中的“枪”): ct_run -suite twitter_SUITE.erl -logdir ./results -pa /home/user/gun/deps/*/ebin /home/user/gun/ebin

于 2014-09-09T04:18:18.450 回答