使用Elixir来优化Erlang的单元测试用例
作者:卓一抗 发布时间:[ 2016/8/15 13:53:27 ] 推荐标签:测试用例 单元测试
依然抓瞎:
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 ~.
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
iOS单元测试mocha、chai、sinon和istanbul实现百分之百的单元测试覆盖率关于单元测试的总结及思考编写更好的Java单元测试的7个技巧Android单元测试框架Robolectric3.0介绍(一)使用Kiwi单元测试总结单元测试如此重要,为什么你不知道Python单元测试??使用装饰器实现测试跳过和预期故障对Controller的单元测试写好单元测试的10个技巧单元测试的重要性Angular单元测试系列??Component、Directive、Pipe 以及ServiceAndroid单元测试的整理提升单元测试体验的利器--Mockito使用总结iOS UnitTest单元测试Vue的单元测试探索(二)
更新发布
常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11系统性能测试及调优前期准备
2021/4/15 14:41:29国内比较好用的5款测试管理工具
2021/3/25 17:23:31热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南