Power Assert makes test results easier to understand, without changing your ExUnit test code.
Example test is here:
test "Enum.at should return the element at the given index" do
array = [1, 2, 3, 4, 5, 6]; index = 2; two = 2
assert array |> Enum.at(index) == two
end
Here is the difference between ExUnit and Power Assert results:
Enjoy :muscle: !
Add Power Assert to your mix.exs
dependencies:
defp deps do
[{:power_assert, "~> 0.2.0", only: :test}]
end
and fetch $ mix deps.get
.
Replace use ExUnit.Case
into use PowerAssert
in your test code:
## before(ExUnit)
defmodule YourAwesomeTest do
use ExUnit.Case # <-- **HERE**
end
## after(PowerAssert)
defmodule YourAwesomeTest do
use PowerAssert # <-- **REPLACED**
end
Done! You can run $ mix test
.
Insert use PowerAssert
with ExUnit.CaseTemplate.using/2
macro:
## before(ExUnit.CaseTemplate)
defmodule YourAwesomeTest do
use ExUnit.CaseTemplate
end
## after(PowerAssert)
defmodule YourAwesomeTest do
use ExUnit.CaseTemplate
# add the following
using do
quote do
use PowerAssert
end
end
end
use ExUnit.Case
$ git grep -l 'use ExUnit\.Case' | xargs sed -i.bak -e 's/use ExUnit\.Case/use PowerAssert/g'
Append use PowerAssert
after use ExSpec
:
defmodule ExSpecBasedTest do
use ExSpec
use PowerAssert # <-- append
describe "describe" do
it "it" do
assert something == "hoge"
end
end
end
See also: test/ex_spec/ex_spec_test.exs
Only provide assert
macro:
assert(expression, message \\ nil)
assert List.first(x = [false])
assert fn(x) -> x == 1 end.(2)
<< x :: bitstring >>
"#{x} hoge"
~w(hoge fuga)
assert quote(@opts, do: :hoge)
<<@x, "y">>; <<x :: binary, "y">>
__MODULE__.Foo
Takayuki Matsubara (@ma2ge on twitter)
Distributed under the Apache 2 License.
Check LICENSE files for more information.