依然抓瞎:
rebar eunit
==> rebar_mix_test (eunit)
mix_worker_tests: add_test (module 'mix_worker_tests')...*failed*
in function mix_worker_tests:add_test/0 (test/mix_worker_tests.erl, line 7)
**error:{badmatch,{error,{"no such file or directory","rebar_mix_test.app"}}}
output:<<"">>
=======================================================
Failed: 1.  Skipped: 0.  Passed: 0.
ERROR: One or more eunit tests failed.
ERROR: eunit failed while processing /Users/zhuoyikang/source/services/rebar_mix_test: rebar_abort
  那么怎么办呢,要知道erlang里面主要的是进程,无法直接在eunit启动整个环境来跑测试,那要怎么写?
  答案是我没什么好办法,只能自己写一些脚本,先把这个otp应用启动起来,然后尝试跑那些用例函数,但是这个时候基本上无法使用rebar eunit执行,需要变成自己的命令执行,总的来说是
  你需要自己搞定你的测试框架,只是可以使用eunit的测试函数。
  每次到这个时候我听到很多人骂erlang了,但是作为一名erlang程序员我无力反驳,直到我近学习了一下elixir/mix。
  使用mix 管理应用并编写测试用例
  elixir/mix对单元测试用例的编写非常友好,这里我使用mix新建一个同名应用:
rebar_mix_test cd ..
services mix new rebar_mix_test
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/rebar_mix_test.ex
* creating test
* creating test/test_helper.exs
* creating test/rebar_mix_test_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd rebar_mix_test
mix test
Run "mix help" for more commands.
services cd rebar_mix_test
ls
README.md config    ebin      lib       mix.exs   src       test
  执行单元测试
mix test
Compiled src/rebar_mix_test_app.erl
Compiled src/rebar_mix_test_sup.erl
Compiled src/mix_worker.erl
Compiled lib/rebar_mix_test.ex
Generated rebar_mix_test app
Consolidated Collectable
Consolidated List.Chars
Consolidated String.Chars
Consolidated Enumerable
Consolidated IEx.Info
Consolidated Inspect
.
Finished in 0.1 seconds (0.1s on load, 0.00s on tests)
1 test, 0 failures
Randomized with seed 193760
  里面默认有一个demo测试,内容如下:
  defmodule RebarMixTestTest do
  use ExUnit.Case
  doctest RebarMixTest
  test "the truth" do
  assert 1 + 1 == 2
  end
  end
  修改为:
  defmodule RebarMixTestTest do
  use ExUnit.Case
  doctest RebarMixTest
  test "the add" do
  assert :mix_worker.add(1,1) == 2
  end
  end
  接下来修改mix.ex,加了一行:mod: {:rebar_mix_test_app, []},
# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[
mod: {:rebar_mix_test_app, []},
applications: [:logger]
]
end
  这样代码会在应用启动时自动执行,由于Elixir调用erlang的代码模块是需要在前面加:,所以这里其实是启动了我们之前编写的otp应用rebar_mix_test。
  然后执行mix test,一片大好:
mix test
Compiled lib/rebar_mix_test.ex
Generated rebar_mix_test app
Consolidated List.Chars
Consolidated Collectable
Consolidated String.Chars
Consolidated Enumerable
Consolidated IEx.Info
Consolidated Inspect
.
Finished in 0.04 seconds (0.04s on load, 0.00s on tests)
1 test, 0 failures
Randomized with seed 55281 
  Elixir的ExUnit
  ExUnit是Elixir的单元测试框架,也是我们刚才小小试用的那个,这个工具非常强大,再演示一个callback功能。
  我们常常需要在每一个测试用例开始执行前进行一些初始化,或许是在整个测试用例文件开始执行前进行一些初始化。一般来讲写出来的eunit代码大概是这样。
  不得不在每一个函数前增加初始化代码,比如:
  a_test() ->
  {ok,Pid} =  KV.Registry.start_link(context.test)
  ... do some logic with pid .
  ok.
  b_test() ->
  {ok,Pid} =  KV.Registry.start_link(context.test)
  ... do some logic with pid .
  ok.
  ...
  而在ExUnit中你可以这样写:
defmodule KV.RegistryTest do
use ExUnit.Case, async: true
setup do
{:ok, registry} = KV.Registry.start_link
{:ok, registry: registry}
end
test "spawns buckets", %{registry: registry} do
assert KV.Registry.lookup(registry, "shopping") == :error
KV.Registry.create(registry, "shopping")
assert {:ok, bucket} = KV.Registry.lookup(registry, "shopping")
KV.Bucket.put(bucket, "milk", 1)
assert KV.Bucket.get(bucket, "milk") == 1
end
test "removes buckets on exit", %{registry: registry} do
KV.Registry.create(registry, "shopping")
{:ok, bucket} = KV.Registry.lookup(registry, "shopping")
Agent.stop(bucket)
assert KV.Registry.lookup(registry, "shopping") == :error
end
end
  是不是很方便?
  本文权当抛砖引玉,接下来的功能我也开始去做文档学习了。
  ExUnit
  have fun ~.