使用公共测试测试基于cowboy的Erlang应用程序

使用公共测试测试基于cowboy的Erlang应用程序,erlang,cowboy,common-test,Erlang,Cowboy,Common Test,我有一个基于Cowboy的Erlang应用程序,我想测试它 以前我使用wooga的库来完成这类任务,但我想开始使用通用测试,因为我注意到这是在cowboy中使用的方法。我试图设置一个非常基本的测试,但无法正常运行 有人能给我提供一个测试基本示例的示例,并告诉我使用示例中包含的Makefile从控制台运行测试的正确方法吗?示例make仅用于构建echo\u get应用程序。因此,要测试echo_get应用程序,您可以添加测试套件并从shell调用make&&rebar-v1 skip_deps=t

我有一个基于Cowboy的Erlang应用程序,我想测试它

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


有人能给我提供一个测试基本示例的示例,并告诉我使用示例中包含的Makefile从控制台运行测试的正确方法吗?

示例make仅用于构建echo\u get应用程序。因此,要测试echo_get应用程序,您可以添加测试套件并从shell调用
make&&rebar-v1 skip_deps=true ct
(钢筋应位于路径中)。 此外,您还需要在Erlang路径中使用etest和etest_http,或者将其与rebar.config一起添加到应用程序中。您可以将httpc或curl与os:cmd/1一起使用,而不是ehttp\u test:)

test/my\u test\u SUITE.erl()


下面启动hello_world应用程序及其依赖项,但使用最新编译的版本,而不是。/\u rel;这可能是您想要的,也可能不是您想要的,但它确实避免了
计时器:sleep(1000)

-模块(hello\u world\u套件)。
-include_lib(“common_test/include/ct.hrl”)。
-导出([all/0,每个套件初始化/1,每个套件结束/1])。
-导出([http\u get\u hello\u world/1])。
全部()->
[http\u get\u hello\u world]。
初始化每个套件(配置)->
{好的,App_Start_List}=Start([hello_world]),
inets:start(),
[{app_start_list,app_start_list}}配置]。
每个套件结束(配置)->
inets:stop(),
停止(?配置(应用程序启动列表,配置)),
配置。
http\u get\u hello\u world(\u Config)->
{ok,{{{u Version,200,{u ReasonPhrase},{u Headers,Body}=
httpc:请求(get,{”http://localhost:8080", []}, [], []),
Body=“你好,世界!\n”。
启动(应用)->
{好的,开始(_To_start=Apps,_start=[])}。
开始([],开始)->
起动;
是否启动([App | Apps],已启动)->
案例应用:开始(应用程序)
好的->
启动(应用程序,[应用程序启动]);
{错误,{未启动,Dep}}->
do|u start([Dep |[App | Apps]],已启动)
结束。
停止(应用)->
_=[应用:停止(应用)| |应用使用

要运行提供的示例(考虑“用户”文件夹中的“枪”):

ct_run-suite twitter_suite.erl-logdir./results-pa/home/user/gun/deps/*/ebin/home/user/gun/ebin

echo\u-get不包含任何通用测试套件。Makefile也没有运行通用测试套件的部分。因此,如果要使用ct对其进行测试,必须实现一个测试套件。要运行套件,可以使用标准的erlang实用工具ct\u-run,也可以尝试使用rebar()。我知道echo_get不包含任何测试。我在询问示例。由于某些原因,该版本未启动。我尝试更改到os:cmd(“../../../\u rel/bin/echo_get_example[start或stop]”)的路径,但它也不起作用。通过在不同的shell中运行该版本并运行测试,它可以工作(甚至可以使用stop命令).知道吗?这意味着什么?发布没有启动?一些shell输出或日志将对您有所帮助。我的意思是,所有测试都会失败,因为服务器没有运行。我可以通过设置一个计时器来解决此问题:sleep(1000)在os:cmd/1命令之后…但我认为这不是正确的方法…这是一个品味问题。在任何情况下,都没有其他启动应用程序的方法。您可以创建启动应用程序然后启动测试的shell脚本(
/rel/bin/echo\u get\u示例start&&rebar skip\u deps=true ct
).但这与您的测试用例所做的相同。
-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.
-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.