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 `true` if the number is even, otherwise `false`.
## 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
Then, you can then run your tests with mix test
.