Skip to content

ExUnit's capture_log silences noisy log output inside a test

December 5, 2025

If a test triggers noisy log output, just wrap the noisy function in ExUnit.CaptureLog.capture_log/2:

import ExUnit.CaptureLog

test "logs a warning" do
  log = capture_log(fn ->
    MyApp.noisy_function()
  end)

  assert log =~ "something went wrong"
end

For the whole test, there’s a tag:

@tag capture_log: true
test "something that logs warnings" do
  assert MyApp.noisy_function() == :ok
end

Or turn it on for all tests in test/test_helper.exs:

ExUnit.start(capture_log: true)

Either way, the captured logs still show up in the failure report when a test fails.

—

Back to TIL

Next: Bun has built-in parallel and sequential script running

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace