Erlang 测试套件
TestKit for Erlang
有没有类似Akka中TestKit的Erlang测试框架?
目标是在集成环境中测试进程,例如,在一端向一组进程发送一些消息,并断言在另一端输出的结果消息。 Akka Testkit 使这些类型的测试相当简单,但我还没有在 Erlang 中找到等效项。
编辑:作为我正在寻找的最简单的例子,假设我们有一个进程 A 需要向进程 B 发送消息,我想测试这种行为。
在 Akka 中,我可以基于 TestKit class 实例化一个 actor,它有一个内置方法 expectMsg
。所以我的测试看起来像这样:
- 实例化模拟 B actor
- 实例化 A actor(以某种方式获取对 B 的引用)
- 给B发消息
- 调用 B.expectMsg 验证它是否收到消息(注意
- 这会自动确保不会向 B 发送其他类型的消息,并且
- 您可以选择提供超时)
Erlang 中有支持这种工作流的库吗?据我所知,EUnit 和 CT 都不支持这种测试。
要了解更复杂的断言,请参阅此页面:http://doc.akka.io/api/akka/2.0/akka/testkit/TestKit.html
Erlang 本身使这些类型的测试相当简单。有用于大规模集成测试的 Lightweight Unit Testing Framework for Erlang eunit
and there is Common Test 框架。
编辑:
对于如此简单的事情,您只需要 Erlang:
$ cat echo.erl
-module(echo).
-export([start/0, send/3]).
start() ->
spawn_link(fun() ->
receive
{To, Msg} -> To ! Msg
end
end).
send(Echo, To, Msg) ->
Echo ! {To, Msg}.
-include_lib("eunit/include/eunit.hrl").
echo_test_() ->
Msg = "Hello world!",
{timeout, 0.1, fun() ->
Echo = echo:start(),
echo:send(Echo, self(), Msg),
?assertEqual(Msg, receive X -> X end)
end}.
$ erlc echo.erl
$ erl
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V7.0 (abort with ^G)
1> eunit:test(echo, [verbose]).
======================== EUnit ========================
echo: echo_test_ (module 'echo')...ok
=======================================================
Test passed.
ok
2>
进程 A
由 echo:start/0
启动,进程 B
正在测试进程本身。如果你想模拟现有模块,有 meck
。如果您只想查看两个进程之间的消息而不想弄乱接收方代码,您当然可以使用 Erlang VM 本身的跟踪功能。
有没有类似Akka中TestKit的Erlang测试框架?
目标是在集成环境中测试进程,例如,在一端向一组进程发送一些消息,并断言在另一端输出的结果消息。 Akka Testkit 使这些类型的测试相当简单,但我还没有在 Erlang 中找到等效项。
编辑:作为我正在寻找的最简单的例子,假设我们有一个进程 A 需要向进程 B 发送消息,我想测试这种行为。
在 Akka 中,我可以基于 TestKit class 实例化一个 actor,它有一个内置方法 expectMsg
。所以我的测试看起来像这样:
- 实例化模拟 B actor
- 实例化 A actor(以某种方式获取对 B 的引用)
- 给B发消息
- 调用 B.expectMsg 验证它是否收到消息(注意
- 这会自动确保不会向 B 发送其他类型的消息,并且
- 您可以选择提供超时)
Erlang 中有支持这种工作流的库吗?据我所知,EUnit 和 CT 都不支持这种测试。
要了解更复杂的断言,请参阅此页面:http://doc.akka.io/api/akka/2.0/akka/testkit/TestKit.html
Erlang 本身使这些类型的测试相当简单。有用于大规模集成测试的 Lightweight Unit Testing Framework for Erlang eunit
and there is Common Test 框架。
编辑: 对于如此简单的事情,您只需要 Erlang:
$ cat echo.erl
-module(echo).
-export([start/0, send/3]).
start() ->
spawn_link(fun() ->
receive
{To, Msg} -> To ! Msg
end
end).
send(Echo, To, Msg) ->
Echo ! {To, Msg}.
-include_lib("eunit/include/eunit.hrl").
echo_test_() ->
Msg = "Hello world!",
{timeout, 0.1, fun() ->
Echo = echo:start(),
echo:send(Echo, self(), Msg),
?assertEqual(Msg, receive X -> X end)
end}.
$ erlc echo.erl
$ erl
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V7.0 (abort with ^G)
1> eunit:test(echo, [verbose]).
======================== EUnit ========================
echo: echo_test_ (module 'echo')...ok
=======================================================
Test passed.
ok
2>
进程 A
由 echo:start/0
启动,进程 B
正在测试进程本身。如果你想模拟现有模块,有 meck
。如果您只想查看两个进程之间的消息而不想弄乱接收方代码,您当然可以使用 Erlang VM 本身的跟踪功能。