Unit Testing with ExUnit
Write fast and reliable unit tests for your Elixir modules and functions using the built-in ExUnit framework.
Intro to ExUnit
Welcome to unit testing with ExUnit! ExUnit is Elixir's built-in testing framework, designed to help you write reliable and maintainable code.
Unit tests focus on small, isolated parts of your code, like individual functions or modules. They ensure each component works as expected, giving you confidence as your application grows.
Your First ExUnit Test
Elixir test files typically end with _test.exs and are placed in a test/ directory. Inside, you use ExUnit.Case to bring in testing functionalities.
A test block starts with test "description" do ... end. Here's a simple example:
defmodule MyMath do
def add(a, b), do: a + b
end
defmodule MyMathTest do
use ExUnit.Case, async: true
test "adds two numbers correctly" do
assert MyMath.add(1, 2) == 3
end
end