When you document your code with @doc, you can supply code examples like so:

# myproject/lib/my_module.exs

defmodule MyModule do
  @doc """
  Given a number, returns trueif the number is even and else otherwise.

  ## Example
    iex> MyModule.even?(2)
    true
    iex> MyModule.even?(3)
    false
  """
  def even?(number) do
    rem(number, 2) == 0
  end
end

You can add the code examples as test cases into one of your test suites:

# myproject/test/doc_test.exs

defmodule DocTest do
  use ExUnit.Case
  doctest MyModule
end

Run your tests with mix test.

Additional reading: