0

My tests' ExUnit.Callbacks.setup function creates an ID that I want to include in ExUnit's output when a test fails. Is there a simple way to do this? I know that I could write a custom ExUnit.Formatter, but that seems like overkill.

The context of this question is that I'm using ExUnit to help write what are not unit tests but integration tests that span multiple microservices. The ID that the setup function generates is a Spandex trace ID that persists across microservices. When a test fails, I want to know what its trace ID was so that I can grep all the microservices' logs for that ID.

1 Answers1

3

One way to do that is to use the message parameter of assert and friends. The message will only be shown when the assertion fails.

setup do
  # Generate a trace id and pass it to each test.
  %{trace_id: Spandex.new_trace_id()}
end

test "something", %{trace_id: trace_id} do
  response = SomeService.do_something(trace_id)

  # Use the failure message to display the trace id.
  assert response[:body] == "ok", "Failed with trace id: #{trace_id}"
end
zwippie
  • 15,050
  • 3
  • 39
  • 54