【问题标题】:Testing Erlang applications based on cowboy using common test使用 common test 测试基于牛仔的 Erlang 应用程序
【发布时间】:2013-11-05 15:40:03
【问题描述】:

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

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

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

【问题讨论】:

  • echo_get 不包含任何常见的测试套件。 Makefile 也没有运行通用测试套件的部分。所以如果你想用 ct 测试它,你必须实现一个测试套件。要运行套件,您可以使用标准 erlang 实用程序 ct_run 或者您可以尝试 rebar (github.com/basho/rebar)。
  • 我知道 echo_get 不包含任何测试。我在问例子。

标签: erlang cowboy common-test


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    以下启动 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.
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 由于某些原因没有开始发布。我试图更改为 os:cmd("../../_rel/bin/echo_get_example [start or stop]") 的路径,但它也不起作用。通过在不同的 shell 中运行发行版并运行它可以工作的测试(甚至是停止命令)。有什么想法吗?
      • 什么意思——发布没有开始?一些 shell 输出或日志将对您有所帮助。
      • 我的意思是,所有测试都失败了,因为服务器没有运行。我可以通过在 os:cmd/1 命令之后放置一个 timer:sleep(1000) 来解决它……但我相信这不是正确的方法……
      • 这是一个品味问题。无论如何,没有其他方法可以启动应用程序。您可以创建启动应用程序然后启动测试的 shell 脚本 (./rel/bin/echo_get_example start &amp;&amp; rebar skip_deps=true ct)。但这与您的测试用例已经完成的相同。
      猜你喜欢
      • 2016-03-07
      • 2015-07-21
      • 2019-05-09
      • 2013-09-17
      • 2018-10-17
      • 2020-09-05
      • 2017-09-04
      • 2021-05-09
      • 1970-01-01
      相关资源
      最近更新 更多