背景:gen_fsm 是Erlang的有限状态机behavior,很实用。爱立信的一位TDD大神写了一篇怎样測试gen_fsm,这个fsm是一个交易系统,负责简单的交易员登陆,插入item,删除item等等,翻译例如以下:

1. Start and Stop

先看下最初版本号的tradepost_tests:

-module(tradepost_tests).
-include_lib("eunit/include/eunit.hrl").
% This is the main point of "entry" for my EUnit testing.
% A generator which forces setup and cleanup for each test in the testset
main_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
% Note that this must be a List of TestSet or Instantiator
% (I have instantiators == functions generating tests)
[
% First Iteration
fun started_properly/1,
]}. % Setup and Cleanup
setup() -> {ok,Pid} = tradepost:start_link(), Pid.
cleanup(Pid) -> tradepost:stop(Pid). % Pure tests below
% ------------------------------------------------------------------------------
% Let's start simple, I want it to start and check that it is okay.
% I will use the introspective function for this
started_properly(Pid) ->
fun() ->
? assertEqual(pending,tradepost:introspection_statename(Pid)),
?assertEqual([undefined,undefined,undefined,undefined,undefined],
tradepost:introspection_loopdata(Pid))
end.

译者注:在eunit中。 setup返回的值作为全部函数包含cleanup的输入,这里是Pid。

started_properly函数是assert 初始为pending, State的值全为空。

如今Test 还不能run。由于tradepost:introspection_statename(Pid) 和 tradepost:introspection_loopdata(Pid)这两个函数还没有。

于是在tradepost.erl里增加:

introspection_statename(TradePost) ->
gen_fsm:sync_send_all_state_event(TradePost,which_statename).
introspection_loopdata(TradePost) ->
gen_fsm:sync_send_all_state_event(TradePost,which_loopdata).
stop(Pid) -> gen_fsm:sync_send_all_state_event(Pid,stop). handle_sync_event(which_statename, _From, StateName, LoopData) ->
{reply, StateName, StateName, LoopData};
handle_sync_event(which_loopdata, _From, StateName, LoopData) ->
{reply,tl(tuple_to_list(LoopData)),StateName,LoopData};
handle_sync_event(stop,_From,_StateName,LoopData) ->
{stop,normal,ok,LoopData}.

这样就能够run test 了

zen:EUnitFSM zenon$ erl -pa ebin/
Erlang R13B04 (erts-5.7.5) [source] [64-bit] [smp:4:4] [rq:4]
[async-threads:0] [hipe] [kernel-poll:false] Eshell V5.7.5 (abort with ^G)
1> eunit:test(tradepost,[verbose]).
======================== EUnit ========================
module 'tradepost'
module 'tradepost_tests'
tradepost_tests: started_properly...ok
[done in 0.004 s]
[done in 0.005 s]
=======================================================
Test passed.
ok
2>

2. 增加測试用例(identify_seller。 insert_item。 withdraw_item)

identify_seller seller是登陆函数。 insert_item。 withdraw_item是添加。删除item的函数

% This is the main point of "entry" for my EUnit testing.
% A generator which forces setup and cleanup for each test in the testset
main_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
% Note that this must be a List of TestSet or Instantiator
% (I have instantiators)
[
% First Iteration
fun started_properly/1,
% Second Iteration
fun identify_seller/1,
fun insert_item/1,
fun withdraw_item/1
]}. % Now, we are adding the Seller API tests
identify_seller(Pid) ->
fun() ->
% From Pending, identify seller, then state should be pending
% loopdata should now contain seller_password
?assertEqual(pending,tradepost:introspection_statename(Pid)),
?assertEqual(ok,tradepost:seller_identify(Pid,seller_password)),
? assertEqual(pending,tradepost:introspection_statename(Pid)),
? assertEqual([undefined,undefined,seller_password,undefined,
undefined],tradepost:introspection_loopdata(Pid))
end. insert_item(Pid) ->
fun() ->
% From pending and identified seller, insert item
% state should now be item_received, loopdata should now contain itm
tradepost:introspection_statename(Pid),
tradepost:seller_identify(Pid,seller_password),
?assertEqual(ok,tradepost:seller_insertitem(Pid,playstation,
seller_password)),
?assertEqual(item_received,tradepost:introspection_statename(Pid)),
?assertEqual([playstation,undefined,seller_password,undefined,
undefined],tradepost:introspection_loopdata(Pid))
end. withdraw_item(Pid) ->
fun() ->
% identified seller and inserted item, withdraw item
% state should now be pending, loopdata should now contain only password
tradepost:seller_identify(Pid,seller_password),
tradepost:seller_insertitem(Pid,playstation,seller_password),
?assertEqual(ok,tradepost:withdraw_item(Pid,seller_password)),
?assertEqual(pending,tradepost:introspection_statename(Pid)),
?assertEqual([undefined,undefined,seller_password,undefined,
undefined],tradepost:introspection_loopdata(Pid))
end.

在tradepost.erl添加对应的函数:

%%-------------------------------------------------------------------
%%% @author Gianfranco <zenon@zen.home>
%%% @copyright (C) 2010, Gianfranco
%%% Created : 2 Sep 2010 by Gianfranco <zenon@zen.home>
%%%-------------------------------------------------------------------
-module(tradepost).
-behaviour(gen_fsm). %% API
-export([start_link/0,introspection_statename/1,introspection_loopdata/1,
stop/1,seller_identify/2,seller_insertitem/3,withdraw_item/2]). %% States
-export([pending/2,pending/3,item_received/3]). %% gen_fsm callbacks
-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3,
terminate/3, code_change/4]).
-record(state, {object,cash,seller,buyer,time}). %%% API
start_link() -> gen_fsm:start_link(?MODULE, [], []). introspection_statename(TradePost) ->
gen_fsm:sync_send_all_state_event(TradePost,which_statename).
introspection_loopdata(TradePost) ->
gen_fsm:sync_send_all_state_event(TradePost,which_loopdata).
stop(Pid) -> gen_fsm:sync_send_all_state_event(Pid,stop). seller_identify(TradePost,Password) ->
gen_fsm:sync_send_event(TradePost,{identify_seller,Password}).
seller_insertitem(TradePost,Item,Password) ->
gen_fsm:sync_send_event(TradePost,{insert,Item,Password}). withdraw_item(TradePost,Password) ->
gen_fsm:sync_send_event(TradePost,{withdraw,Password}). %%--------------------------------------------------------------------
pending(_Event,LoopData) -> {next_state,pending,LoopData}. pending({identify_seller,Password},_Frm,LoopD = #state{seller=Password}) ->
{reply,ok,pending,LoopD};
pending({identify_seller,Password},_Frm,LoopD = #state{seller=undefined}) ->
{reply,ok,pending,LoopD#state{seller=Password}};
pending({identify_seller,_},_,LoopD) ->
{reply,error,pending,LoopD}; pending({insert,Item,Password},_Frm,LoopD = #state{seller=Password}) ->
{reply,ok,item_received,LoopD#state{object=Item}};
pending({insert,_,_},_Frm,LoopD) ->
{reply,error,pending,LoopD}. item_received({withdraw,Password},_Frm,LoopD = #state{seller=Password}) ->
{reply,ok,pending,LoopD#state{object=undefined}};
item_received({withdraw,_},_Frm,LoopD) ->
{reply,error,item_received,LoopD}. %%--------------------------------------------------------------------
handle_sync_event(which_statename, _From, StateName, LoopData) ->
{reply, StateName, StateName, LoopData};
handle_sync_event(which_loopdata, _From, StateName, LoopData) ->
{reply,tl(tuple_to_list(LoopData)),StateName,LoopData};
handle_sync_event(stop,_From,_StateName,LoopData) ->
{stop,normal,ok,LoopData};
handle_sync_event(_E,_From,StateName,LoopData) ->
{reply,ok,StateName,LoopData}. %%--------------------------------------------------------------------
init([]) -> {ok, pending, #state{}}.
handle_event(_Event, StateName, State) ->{next_state, StateName, State}.
handle_info(_Info, StateName, State) -> {next_state, StateName, State}.
terminate(_Reason, _StateName, _State) -> ok.
code_change(_OldVsn, StateName, State, _Extra) -> {ok, StateName, State}.

再run tests:

zen:EUnitFSM zenon$ erlc -o ebin/ src/*.erl test/*.erl
zen:EUnitFSM zenon$ erl -pa ebin/ -eval 'eunit:test(tradepost,[verbose]).'
Erlang R13B04 (erts-5.7.5) [source] [64-bit] [smp:4:4] [rq:4]
[async-threads:0] [hipe] [kernel-poll:false] Eshell V5.7.5 (abort with ^G)
1> ======================== EUnit ========================
module 'tradepost'
module 'tradepost_tests'
tradepost_tests: started_properly...ok
tradepost_tests: identify_seller...ok
tradepost_tests: insert_item...ok
tradepost_tests: withdraw_item...ok
[done in 0.015 s]
[done in 0.015 s]
=======================================================
All 4 tests passed.
1>

3. 使用eunit_fsm

eunit_fsm是作者写的一个module,使gen_fsm的測试看起来更美观:

原来版本号:

started_properly(Pid) ->
fun() ->
? assertEqual(pending,tradepost:introspection_statename(Pid)),
? assertEqual([undefined,undefined,undefined,undefined,undefined],
tradepost:introspection_loopdata(Pid))
end.

新版本号:

started_properly(Pid) ->
{"Proper startup test",
[{statename,is,pending},
{loopdata,is,[undefined,undefined,undefined,undefined,undefined]}
]}.

再看insert_item, 原来版本号:

insert_item(Pid) ->
fun() ->
% From pending and identified seller, insert item
% state should now be item_received, loopdata should now contain itm
tradepost:introspection_statename(Pid),
tradepost:seller_identify(Pid,seller_password),
?assertEqual(ok,tradepost:seller_insertitem(Pid,playstation,
seller_password)),
?assertEqual(item_received,tradepost:introspection_statename(Pid)),
? assertEqual([playstation,undefined,seller_password,undefined,
undefined],tradepost:introspection_loopdata(Pid))
end.

新版本号:

insert_item(Pid) ->
{"Insert Item Test",
[{state,is,pending},
{call,tradepost,seller_identify,[Pid,seller_password],ok},
{call,tradepost,seller_insertitem,[Pid,playstation,seller_password]},
{state,is,item_received},
{loopdata,is,[playstation,undefined,seller_password,undefined,undefined]}
]}.

看起来更易读了吧!

来看下整个的tradepost_test.erl

-module(tradepost_tests).
-include_lib("eunit/include/eunit.hrl").
-include("include/eunit_fsm.hrl"). % This is the main point of "entry" for my EUnit testing.
% A generator which forces setup and cleanup for each test in the testset
main_test_() ->
{foreach,
fun setup/0,
fun cleanup/1,
% Note that this must be a List of TestSet or Instantiator
[
% First Iteration
fun started_properly/1,
% Second Iteration
fun identify_seller/1,
fun insert_item/1,
fun withdraw_item/1
]}. % Setup and Cleanup
setup() -> {ok,Pid} = tradepost:start_link(), Pid.
cleanup(Pid) -> tradepost:stop(Pid). % Pure tests below
% ------------------------------------------------------------------------------
% Let's start simple, I want it to start and check that it is okay.
% I will use the introspective function for this
started_properly(Pid) ->
?fsm_test(tradepost,Pid,"Started Properly Test",
[{state,is,pending},
{loopdata,is,[undefined,undefined,undefined,undefined,undefined]}
]). % Now, we are adding the Seller API tests
identify_seller(Pid) ->
?fsm_test(Pid,"Identify Seller Test",
[{state,is,pending},
{call,tradepost,seller_identify,[Pid,seller_password],ok},
{state,is,pending},
{loopdata,is,[undefined,undefined,seller_password,undefined,undefined]}
]). insert_item(Pid) ->
?fsm_test(Pid,"Insert Item Test",
[{state,is,pending},
{call,tradepost,seller_identify,[Pid,seller_password],ok},
{call,tradepost,seller_insertitem,[Pid,playstation,seller_password],ok},
{state,is,item_received},
{loopdata,is,[playstation,undefined,seller_password,undefined,undefined]}
]). withdraw_item(Pid) ->
? fsm_test(Pid,"Withdraw Item Test",
[{state,is,pending},
{call,tradepost,seller_identify,[Pid,seller_password],ok},
{call,tradepost,seller_insertitem,[Pid,button,seller_password],ok},
{state,is,item_received},
{call,tradepost,seller_withdraw_item,[Pid,seller_password],ok},
{state,is,pending},
{loopdata,is,[undefined,undefined,seller_password,undefined,undefined]}
]).

在这里我们看下作者自己写的 eunit_fsm.hrl 和  eunit_fsm.erl

eunit_fsm.hrl :

-define(fsm_test(Id,Title,CmdList),
{Title,fun() -> [ eunit_fsm:translateCmd(Id,Cmd) || Cmd <- CmdList] end}).

eunit_fsm.erl:

-module(eunit_fsm).
-export([translateCmd/2,get/2]).
-define(Expr(X),??X).
translateCmd(Id,{state,is,X}) ->
case get(Id,"StateName") of
X -> true;
_V -> .erlang:error({statename_match_failed,
[{module, ?MODULE},
{line, ? LINE},
{expected, X},
{value, _V}]})
end;
translateCmd(_Id,{call,M,F,A,X}) ->
case apply(M,F,A) of
X -> ok;
_V -> .erlang:error({function_call_match_failed,
[{module, ?MODULE},
{line, ?LINE},
{expression, ?Expr(apply(M,F,A))},
{expected, X},
{value, _V}]})
end;
translateCmd(Id,{loopdata,is,X}) ->
case tl(tuple_to_list(get(Id,"StateData"))) of
X -> true;
_V -> .erlang:error({loopdata_match_failed,
[{module, ?MODULE},
{line, ?LINE},
{expected, X},
{value, _V}]})
end. % StateName or StateData
get(Id,Which) ->
{status,_Pid,_ModTpl, List} = sys:get_status(Id),
AllData = lists:flatten([ X || {data,X} <- lists:last(List) ]),
proplists:get_value(Which,AllData).

看下如今的文件夹结构:

zen:EUnitFSM zenon$ tree .
.
├── ebin
├── include
│ └── eunit_fsm.hrl
├── src
│ └── tradepost.erl
└── test
├── eunit_fsm.erl
└── tradepost_tests.erl 4 directories, 4 files

来编译后Run一下:

zen:EUnitFSM zenon$ erlc -o ebin/ src/*.erl test/*.erl
zen:EUnitFSM zenon$ erl -pa ebin/ -eval 'eunit:test(tradepost,[verbose]).'
Erlang R13B04 (erts-5.7.5) [source] [64-bit] [smp:4:4] [rq:4]
[async-threads:0] [hipe] [kernel-poll:false] Eshell V5.7.5 (abort with ^G)
1> ======================== EUnit ========================
module 'tradepost'
module 'tradepost_tests'
tradepost_tests: started_properly (Started Properly Test)...[0.001 s] ok
tradepost_tests: identify_seller (Identify Seller Test)...ok
tradepost_tests: insert_item (Insert Item Test)...ok
tradepost_tests: withdraw_item (Withdraw Item Test)...ok
[done in 0.014 s]
[done in 0.014 s]
=======================================================
All 4 tests passed. 1>

全Pass。

Erlang学习: EUnit Testing for gen_fsm的更多相关文章

  1. Erlang 学习笔记

    http://wenku.baidu.com/link?url=AUQR8Hn-e-fEB_lqjXsd8XfapWj1qAK7J05JoBXFib_LlSk5qSOTia8HIxNV1XkeZi-k ...

  2. erlang四大behaviour之二-gen_fsm

    来源:http://www.cnblogs.com/puputu/articles/1701012.html 今天介绍erlang的一个非常重要的behaviour,就是gen_fsm-有限状态机,有 ...

  3. erlang学习笔记(shell命令)

    erlang shell 命令: help(). 可以查看erlang shell内置命令. 比如:m(Mod),可以查看模块Mod. 待续..

  4. Programming Erlang 学习笔记(一)

    入门 启动Shell 在cmd中输入命令”erl”,百分号(%)表示一个注释的开始,从百分号开始到这行结束的所有文本都被看做是注释. 一个完整的命令需要以一个句点和一个回车结束. 退出erlang的命 ...

  5. erlang学习笔记之基础语法

    字符串是双引号,单引号的是atom元组: 下标从1开始 X = {'test1',2,3,4}. element(1,X). 配合模式匹配,可以给元素项命名,直接不用下标标记元素项 列表增删改查 增加 ...

  6. Erlang学习记录(三)——表达式大集合

    Erlang中的表达式必须以.结束才会去执行.如果不加.你在编译环境下按多少次Enter,表达式都不会执行,表达式之间可以用,分隔,以.结尾后所有的表达式都会执行,但是只有最后一个以.结尾的表达式会在 ...

  7. Erlang学习记录(二)——基本数据类型

    Erlang可以说和我以前接触过的语言都大不相同,这个从它的类型定义就可以看出来...反正学起来觉得既不熟悉,也不亲切,我估计在用Erlang写应用的时候,整个编程思路都要变一下了.不过存在即是合理的 ...

  8. Erlang学习记录(一)——Windows下的环境搭建

    一.安装编译器 在http://www.erlang.org/download.html下载R16B01 Windows Binary File并安装. 二.运行编译器 安装完编译器后,打开安装目录下 ...

  9. Erlang学习笔记2

    http://wgcode.iteye.com/blog/1007623 第二章 入门 1.所有的变量都必须以大写字母开头,如果要查看某个变量,只要输入变量的名字即可,如果一个变量被赋予值,就称为绑定 ...

随机推荐

  1. MS Server中varchar与nvarchar的区别

    很多时候我们在创建数据库时在给字段设置数据类型时会选择varchar或是nvarchar.当然还可以选择别的数据类型,本文只对varchar和nvarchar两种类型做说明.如下测试表Test的表结构 ...

  2. Html.Partial("")与Html.RenderPartial("")区别

    文章有点长,但大多是代码,看看很快的,不要压力太大.网上有很多关于这两个方法的区别,都说出了它本质的区别(不看代码,只看这个结论,就已经足够了,如果觉得有必要从代码中得出这个结论,那就继续往下看),这 ...

  3. Scanner类及正则表达式

    import java.util.Scanner; public class ScannerToString { public static void main(String[] args) { Sc ...

  4. App开发所要注意的几个法务问题(转)

    GameLook 报道/ 移动应用市场的飞速发展催生出大量揭竿而起的开发者,同时许多矛盾也渐渐明显起来.其中涉及“抄袭”的问题尤为突出,毫不客气地说对于那些有底子的游戏厂商来说,法务已经成为团队中的一 ...

  5. UVA 839 (13.08.20)

     Not so Mobile  Before being an ubiquous communications gadget, a mobile wasjust a structure made of ...

  6. POJ 3619 Speed Reading(简单题)

    [题意简述]:有K头牛,N页书,每次第i头牛每分钟仅仅能读Si页书,连续读Ti分钟,之后歇息Ri分钟.如今问我们第i头牛花费多少时间能够读完这N页书. [分析]:简单的模拟 //220K 32Ms # ...

  7. Xamarin.forms 自定义tabview控件

    一 问题描述 forms本身ui代码是翻译为平台原生代码,forms按照xaml技术进行对android和ios两种ui模型进行公共抽象出了几种page和view,在空杯博客已经有详细介绍 http: ...

  8. 页面提交错误,页面间参数传递java.lang.NumberFormatException: null

    多次出现这样的错误,在点击一个按钮触发提交整个页面的事件时,总是报错,不止一次出现这样的错误了. 出现这种问题的分析: 1 我们从这个问题的本身来看,java.lang.NumberFormatExc ...

  9. Shell echo命令

    Shell echo命令 echo "It is a test" 这里的双引号完全可以省略 .显示变量 read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shel ...

  10. 循环获取json对象的属性名

    今天做项目遇到一个难题,asp.net 项目,数据库中一个表有八十多个字段,我已经在前台将表转化为了json字符数组,我要在前台循环这八十多个字段,我只能根据属性名来处理,一筹莫展,最终解决,收益颇多 ...