在erlang中测试

时间:2017-09-15 06:38:26

标签: concurrency parallel-processing erlang erlang-shell

我试图在Erlang中学习测试。我使用了这个link的tic-tac-toe游戏的代码库。这是一个双人游戏(让我们把它称为一对玩家)。我基本上想检查当多对玩家同时玩这个游戏时会发生什么。 所以我尝试编写一个基本的和一个简单的模块来测试它,名为testing,如下所示:

-module(testing).

%% ====================================================================
%% API functions
%% ====================================================================
-export([all/0]).



%% ====================================================================
%% Internal functions
%% ====================================================================
all()->

    test1(),
    timer:sleep(75),
    test2(),
    timer:sleep(75),
    test3(),
    timer:sleep(75).





test1()->

    G_serv_Output1=gameserver:start(),
    io:format("Value of G_serv_Output in test1 is ~p~n",
              [G_serv_Output1]),
    Abhishek=gameclient:login("Abhishek"),
    Dharun=gameclient:login("Dharun"),


    gameclient:new_game(Abhishek,"Dharun"),

    timer:sleep(5),

    gameclient:make_move(Abhishek,"Dharun", a1),
    timer:sleep(5),
    gameclient:make_move(Dharun,"Abhishek", b2),
    timer:sleep(5),
    gameclient:make_move(Abhishek,"Dharun", a2),
    timer:sleep(5), 
    gameclient:make_move(Dharun,"Abhishek", c3),
    timer:sleep(5),
    gameclient:make_move(Abhishek,"Dharun", a3).




test2()->

    G_serv_Output1=gameserver:start(),
    io:format("Value of G_serv_Output in test2 is ~p~n",
               [G_serv_Output1]),
    Abhijeet=gameclient:login("Abhijeet"),
    Ranjan=gameclient:login("Ranjan"),


    gameclient:new_game(Abhijeet,"Ranjan"),
    timer:sleep(5),

    gameclient:make_move(Abhijeet,"Ranjan", a1),
    timer:sleep(5),
    gameclient:make_move(Ranjan,"Abhijeet", b2),
    timer:sleep(5),
    gameclient:make_move(Abhijeet,"Ranjan", a2),
    timer:sleep(5), 
    gameclient:make_move(Ranjan,"Abhijeet", c3),
    timer:sleep(5),
    gameclient:make_move(Abhijeet,"Ranjan", a3).





test3()->

    G_serv_Output1=gameserver:start(),
    io:format("Value of G_serv_Output in test3 is ~p~n",
              [G_serv_Output1]),
    Bana=gameclient:login("Bana"),
    Sagar=gameclient:login("Sagar"),


    gameclient:new_game(Bana,"Sagar"),
    timer:sleep(5),

    gameclient:make_move(Bana,"Sagar", a1),
    timer:sleep(5),
    gameclient:make_move(Sagar,"Bana", b2),
    timer:sleep(5),
    gameclient:make_move(Bana,"Sagar", a2),
    timer:sleep(5), 
    gameclient:make_move(Sagar,"Bana", c3),
    timer:sleep(5),
    gameclient:make_move(Bana,"Sagar", a3).

所有三项测试都运行良好,我看到所有三场比赛都完成了。

然后我注意到像gameclient:new_game/2gameclient:new_game/3这样的函数也接受了Opponent name作为输入,我决定将其删除,因此我更改了new_game/2 to new_game/1和{{1在make_move/3 to make_move/2中如下:

gameclient

并在new_game(Pid) -> Pid ! {new_game}. make_move(Pid, Move) -> Pid ! { make_move, Move }. loop(Name) -> receive { new_game} -> gameserver:new_game(Name), loop(Name); { make_move, Move } -> gameserver:make_move(Name, Move), loop(Name) end. 模块中:

gameserver

将两个模块中的其余代码保持不变。

但是现在当我运行模块时,我发现只有test1成功运行,test2和test3没有运行。 我试图找到原因并且我发现当test2和test3开始时,test1的播放器名称仍然在dict中,但是因为我在所有三个中都调用了new_game(Name) -> global:send(?SERVER, { new_game, Name}). make_move(Name, Move) -> global:send(?SERVER, { make_move, Name, Move }). game_loop(Players, Games) -> process_flag(trap_exit, true), receive { new_game, Name} -> PlayerList=dict:fetch_keys(Players), PlayerAndOpponent=lists:partition(fun(A)->Name==A end, PlayerList), {_,OpponentNameList}=PlayerAndOpponent, OpponentName=list_to_tuple(OpponentNameList), case dict:find(Name, Players) of { ok, Pid } -> case dict:find(element(1,OpponentName), Players) of { ok, OpponentPid} -> Pid ! { msg, "Ready to rumble against " ++ element(1,OpponentName) ++ "!" }, OpponentPid ! { msg, "Ready to rumble against " ++ Name ++ "!" }, GamePid = tictactoe:start({Pid, Name}, {OpponentPid, element(1,OpponentName)}), GameKey = create_game_key(Name, element(1,OpponentName)), link(GamePid), game_loop(Players, dict:store(GameKey, GamePid, Games)); error -> Pid ! { msg, "Did not find opponent " ++ OpponentName ++ "!" }, game_loop(Players, Games) end; error -> io:format("Could not find player ~p~n", [ Name ]), game_loop(Players, Games) end; { make_move, Name, Move } -> PlayerList=dict:fetch_keys(Players), PlayerAndOpponent=lists:partition(fun(A)->Name==A end, PlayerList), {_,OpponentNameList}=PlayerAndOpponent, OpponentName=list_to_tuple(OpponentNameList), case dict:find(create_game_key(Name, element(1,OpponentName)), Games) of { ok, GamePid } -> tictactoe:make_move(GamePid, Name, Move), game_loop(Players, Games); error -> game_loop(Players, Games) end end. 测试不应该发生。

问题:

如果我必须为两个用户测试这个tictactoe游戏,但他们必须同时玩,那么我的测试用例模块应该如何?

编辑后 我查看了erlang的common_test库并在同样的问题上实现了它 通过创建新模块gameserver:start/0,如下所示:

testing2

但是当我在ct生成的html日志中看到我仍然无法看到这些游戏正在运行但我看到以下内容 在test1中

-module(testing2).
-include_lib("common_test/include/ct.hrl").
%% ====================================================================
%% API functions
%% ====================================================================
-export([all/0, groups/0, init_per_group/2, end_per_group/2]).
-export([test1/1,test2/1]).



%% ====================================================================
%% Internal functions
%% ====================================================================
all() -> [{group, session}].

groups() -> [{session,
              [],
              [{group, clients}]},
             {clients,
              [parallel, {repeat, 1}],
              [test1, test2]}].

init_per_group(session, Config) ->
   gameserver:start(),
    Config;
init_per_group(_, Config) ->
    Config.

end_per_group(session, _Config) ->
    gameserver:stop();
end_per_group(_, _Config) ->
    ok.

test1(_Config)->



Abhishek=gameclient:login("Abhishek"),
Dharun=gameclient:login("Dharun"),


gameclient:new_game(Abhishek),

    timer:sleep(10),

    gameclient:make_move(Abhishek, a1),
    timer:sleep(10),
    gameclient:make_move(Dharun, b2),
    timer:sleep(10),
    gameclient:make_move(Abhishek, a2),
    timer:sleep(10),    
    gameclient:make_move(Dharun, c3),
    timer:sleep(10),
    gameclient:make_move(Abhishek, a3).





test2(_Config)->



Abhijeet=gameclient:login("Abhijeet"),
Ranjan=gameclient:login("Ranjan"),


gameclient:new_game(Abhijeet),
    timer:sleep(10),

    gameclient:make_move(Abhijeet, a1),
    timer:sleep(10),
    gameclient:make_move(Ranjan, b2),
    timer:sleep(10),
    gameclient:make_move(Abhijeet, a2),
    timer:sleep(10),    
    gameclient:make_move(Ranjan, c3),
    timer:sleep(10),
    gameclient:make_move(Abhijeet, a3).

,在test2中

=== Started at 2017-09-19 11:48:08



"Welcome to tictactoe server!"

"Welcome to tictactoe server!"

"Ready to rumble against Abhishek!"

"Ready to rumble against Dharun!"




=== Ended at 2017-09-19 11:48:08
=== successfully completed test case
=== Returned value: {make_move,a3}

请帮帮我。

0 个答案:

没有答案