Test
ExUnit
ExUnit is Elixir's built-in testing framework. It's integrated with Mix.
A basic example
1. Code to Test (lib/my_math.ex)
defmodule MyMath do
def add(a, b) do
a + b
end
end
2. Test File (test/my_math_test.exs)
defmodule MyMathTest do
use ExUnit.Case
doctest MyMath # Run doctests found in lib/my_math.ex (optional but good practice)
test "adds two numbers" do
assert MyMath.add(1, 2) == 3
assert MyMath.add(0, 0) == 0
assert MyMath.add(-1, 1) == 0
end
end
3. Running Tests
mix test
Mix will compile your code and run all files matching test/*_test.exs. To test a single file:
mix test test/my_math_test.exs
To get coverage reporting:
mix --cover test
More
describesetup
Mock
Mock is a testing library that allows you to temporarily replace the behavior of modules or functions during tests.