Tests and Code Coverage

Get to know about the fundamentals of tests and code coverage in this lesson.

In the past few years, we’ve seen tremendous growth in automated software testing. Elixir embraces this trend. Rather than doing a deep dive into any single tool, we’re going to walk you through a few important ones that will help ease Elixir adoption.

ExUnit

Elixir ships with a unit testing framework called ExUnit. Based on long-standing principles, it serves as the basic building block for almost all other Elixir testing frameworks.

The Elixir community expects applications and libraries to be well tested. We won’t give more than a brief overview here, but we will touch on some ExUnit basics:

  • Tests are a series of scripts that Mix discovers and runs based on their name.
  • Each test runs a flow of setup, test, and teardown.
  • After setup, a test executes some piece of application code and then makes one or more assertions about what should be true.
  • If an assertion is not true or there’s an unplanned application exception, the test fails.

ExUnit has a strong focus on usability. Every time an assertion fails, we get detailed reporting on what went wrong. Recent Elixir versions even show colored diffs in those reports, making it trivial to spot errors.

Most interactions with the test suite happen through mix test. Because it integrates with ExUnit tags, mix test provides plenty of ...