Test Phoenix controllers produces warnings in output

My test:

  @non_existent_profile %{
    "login" => "non-existent-user",
    "pass" => Base.encode64("non-existent-pass")
  }

test "should return 401 for non-existent user", %{conn: conn} do
    assert _ =
      conn
      |> post(Routes.auth_token_path(conn, :create), @non_existent_profile)
      |> json_response(401)
    end
  end

Output in console:

GetmsgApi.AuthTokenControllerTest [test/getmsg_api/controllers/auth_token_controller_test.exs]
  * test should return 401 for non-existent user [L#28]09:23:24.632 [warning] state=set duration=0.113 status=401 params={"login":"non-existent-user","pass":"bm9uLWV4aXN0ZW50LXBhc3M="} path=/getmsg/api/auth-tokens method=POST format=json controller=GetmsgApi.AuthTokenController action=create erl_level=warning application=logster domain=elixir file=lib/logster/plugs/logger.ex function=call/2 line=40 mfa=Logster.Plugs.Logger.call/2 module=Logster.Plugs.Logger pid=<0.1921.0> request_id=F1NOJIEzwahS0w4AAAnh 
  * test should return 401 for non-existent user (3.7ms) [L#28]

I tried configure Logster in TestEndpoint

plug Logster.Plugs.Logger, to: :error

But this didn’t work. How should I do to make the warnings go away?

My app is part of umbrella application. I need to remove warnings only in my app in test env.

I see that log warning is an expected behavior of the app.
We could “capture” log messages… there are multiple ways to do that.

The bare minimum would be to @tag the test with :capture_log as like:

@tag :capture_log
test "should return 401 for non-existent user", %{conn: conn} do
  # code that eventually does Logger.warning()
end

similarly you could set it in @describetag or @moduletag to apply to the set of tests.

Or call ExUnit.start(capture_log: true) to run all tests with captured logs (most probably it’s in your “test_helper.exs”)

2 Likes