Testing with ExUnit

ExUnit is the test framework that ships with Elixir. A test module uses ExUnit.Case, a test is a named block of assertions, and mix test runs every _test.exs file under test/. The Mix projects lesson ran the generated test; this one writes tests by hand.

A first test

The module under test and its test can sit in one file for a small example. The test macro takes a name and a block, and the block runs with the assertions inside it:

test/calc_test.exs

defmodule Calc do def add(a, b), do: a + b end defmodule CalcTest do use ExUnit.Case test "adds two numbers" do assert Calc.add(1, 2) == 3 end test "refuses a wrong sum" do refute Calc.add(1, 2) == 4 end end

elixir -e 'Code.compiler_options(ignore_module_conflict: true); ExUnit.start(); Code.require_file("test/calc_test.exs")'

Running ExUnit with seed: 407356, max_cases: 8

..
Finished in 0.7 seconds (0.7s on load, 0.00s async, 0.00s sync)
2 tests, 0 failures

The seed and the time change on every run; the dots and the count do not. The command starts ExUnit and requires the file, which is what mix test does for every test file of the project. In a Mix project the command is simply mix test.

assert and refute

assert demands a truthy value and refute a falsy one. When the expression is a comparison, ExUnit keeps both sides and prints them on failure, which is the reason a test reports left: 3 and right: 4 rather than a bare false:

setup and context

A setup block runs before each test of the module and returns a map. Every test receives that map as its second argument, so a fixture is built in one place:

setup do returns {:ok, name: "ada"}, and a test declares test "uses the name", %{name: name} do. The key of the map is the name the test asks for. A setup that returns :ok with no map runs before the test and passes no context.

The report of a failing test

A failed assertion prints the test name, the file and line, the code, both sides and the stack. The program below is a broken sum on purpose; the report is what the run prints:

test/broken_test.exs

defmodule Broken do def add(a, b), do: a + b end defmodule BrokenTest do use ExUnit.Case test "adds two numbers" do assert Broken.add(1, 2) == 4 end end

elixir -e 'Code.compiler_options(ignore_module_conflict: true); ExUnit.start(); Code.require_file("test/broken_test.exs")'

Running ExUnit with seed: 840454, max_cases: 8



  1) test adds two numbers (BrokenTest)
     test/broken_test.exs:8
     Assertion with == failed
     code:  assert Broken.add(1, 2) == 4
     left:  3
     right: 4
     stacktrace:
       test/broken_test.exs:9: (test)


Finished in 1.6 seconds (1.5s on load, 0.00s async, 0.05s sync)
1 test, 1 failure

The fix is either the code or the expectation, and the report names both. The doctests lesson writes the same assertions inside the documentation of the function, and the Mix projects lesson runs the whole folder with mix test.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which module does a test module use?

    Answer

    ExUnit.Case

    use ExUnit.Case brings the test macros and the assertions into the module.

  2. What does assert demand of its expression?

    Answer

    A truthy value

    assert accepts any truthy value; a false or nil fails the test.

  3. What does refute demand?

    Answer

    A falsy value

    refute passes when its expression is false or nil.

  4. What does a setup block return to the tests?

    Answer

    A map that each test receives as its context

    The setup returns {:ok, map}, and the map is the test context.

  5. What does this test do? defmodule T do use ExUnit.Case test "sum" do assert 1 + 1 == 2 end end

    Answer

    It passes

    The assertion is true, so the test passes.

  6. What does this test do? defmodule T do use ExUnit.Case test "sum" do refute 1 + 1 == 3 end end

    Answer

    It passes, because the comparison is false

    refute passes on a false expression, and 1 + 1 == 3 is false.

  7. What does a failing assert a == b print?

    Answer

    The code, the left value and the right value

    ExUnit keeps both sides of the comparison for the failure report.

  8. Which part of the ExUnit report changes between two runs?

    Answer

    The seed and the time

    The seed and the elapsed time change; the counts and names stay.

  9. Where do tests live in a Mix project?

    Answer

    test/

    The test folder holds the *_test.exs files and test_helper.exs.

  10. What command runs the tests of a Mix project?

    Answer

    mix test

    mix test starts ExUnit and runs every test file of the project.

Article author: Arthur Isaev