运行“混合测试”时不打印日志

时间:2016-03-31 02:06:30

标签: testing elixir phoenix-framework

我正在尝试调试,而测试没有运行,我有我的测试,我正在尝试打印一些东西,所以我可以在mix test运行时看到元组的值。我试过这样做:

require Logger

test "creates element", %{conn: conn} do
    Logger.debug "debugging #{inspect conn}"
    conn = post conn, v1_content_path(conn, :create), content: @valid_attrs
    ...
    ...
end

但没有打印出来!这让我疯了! 这是我读到的地方,我正在做的事情How to pretty print conn content?

修改还尝试了:

IO.puts "debugging #{inspect conn}"

编辑这里是我的test_helper.exs的内容

ExUnit.start

Mix.Task.run "ecto.create", ~w(-r TestApp.Repo --quiet)
Mix.Task.run "ecto.migrate", ~w(-r TestApp.Repo --quiet)
Ecto.Adapters.SQL.begin_test_transaction(TestApp.Repo)

编辑这是我的整个测试文件:

defmodule TestApp.ContentControllerTest do
  require Logger
  use TestApp.ConnCase

  @valid_attrs %{title: "Content Title", url: "http://www.content.com"}
  @invalid_attrs %{}

  setup %{conn: conn} do
    conn
      |> put_req_header("accept", "application/json")

    {:ok, conn: conn}
  end

  test "my first test", %{conn: conn} do
    Logger.debug "debugging #{inspect conn}"
  end
end

修改以下是mix test的详细信息:

$ mix test
.

Finished in 2.5 seconds (0.6s on load, 1.9s on tests)
1 tests, 0 failures

Randomized with seed 685273

1 个答案:

答案 0 :(得分:13)

compile_time_purge_level

正如您对问题的一些评论中所指出的,通过更改compile_time_purge_level中的:debug配置,:logger可以降低到测试环境的config/test.exs级别。

test.exs

config :logger,
  backends: [:console],
  compile_time_purge_level: :debug

再次运行测试

mix test