How to have clean test output in elixir

August 2, 2017

The Problem

Things other than green dots in elixir test output.

For Example

If you raise an exception on purpose to test the code that’s supposed to handle the error, you’ll see the error output in your console when you run the test. Or if you’re working with a database library and you insert some records to test with, you’ll see the logs showing the records being inserted in your test output. Ew.

The Solution

ExUnit has you covered. Tag the test that’s polluting your test output with @tag :capture_log and you won’t see any output coming from that test anymore, just green dots. 😌.

If you’re testing some module where every test is inserting things into a test database, or is logging things all over the place for other reasons, you can tag your whole module with @moduletag :capture_log to silence the output for every test.

If you want to do something with the captured output instead of just throwing it away, capture_log can return it to you. Check out the ExUnit docs for capture_log to learn more.