Elixir learnings - 2020 week 8

February 17, 2020

Elixir

ExUnit

Disabling tests with a @tag :skip

I've worked in systems where you can specify different groups of test. For example the last place I worked allowed you to mark a test around the specific domain, whether or not it was an integration test or a unit test as well as whether or not it was a historically slow test. It turns out that ExUnit has tagging support built right into it.

I was curious to see if I could target specific groups of tests. Particularly I wanted to exclude some tests that have been flaky. This is natively suppored in ExUnit.

To do so first find your test that you want to exclude.

defmodule App.ExampleTest do
  #...
  test "Some flaky test" do
    # ...
  end
end

Add @tag :skip above the test.

defmodule App.ExampleTest do
  #...
  @tag :skip
  test "Some flaky test" do
    # ...
  end
end

Running mix test at this point wouldn't cause the test to be excluded. You need to make one more change. Go to test/test_helper.exs and then update the ExUnit.start to pass in an exclude argument.

# test/test_helper.exs
# Exclude tests with @tag :skip
ExUnit.start(exclude: [:skip])

As it turns out ExUnit.start/1 accepts the same arguments that ExUnit.configure/1 takes.

Sources

  1. Case templates
  2. Disabling tests with tags
  3. ExUnit.start/1 docs
  4. ExUnit.configure/1 docs

© 2023, Built with ❤️ by Blake Dietz