【问题标题】:Log doesn't print when running "mix test"运行“混合测试”时不打印日志
【发布时间】:2016-04-01 14:20:04
【问题描述】:

我正在尝试在测试未运行时进行调试,我有我的测试并且我正在尝试打印一些东西,以便在运行 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

【问题讨论】:

  • 我不确定 Logger 应用程序在您执行测试时是否正在运行。而不是使用记录器,您是否尝试过简单地执行 IO.puts "debugging #{inspect conn}"
  • 您确定您的记录器级别不高于:debug?您可以阅读有关级别in the Logger docs
  • 您希望输出显示在哪里?它应该显示在控制台上,但是当您与 Phoenix 打交道时,控制台在哪里?
  • @OnorioCatenacci 我期待控制台中的输出显示执行mix test后的测试结果@
  • 您的test_helper.exs 中有import ExUnit.CaptureLog 吗?这将捕获日志而不是将它们写入控制台。我刚刚在本地尝试了 Logger 和 IO,并在我的控制台中看到了两者。

标签: testing elixir phoenix-framework


【解决方案1】:

compile_time_purge_level

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

test.exs

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

再次运行测试

mix test

【讨论】:

  • 您是否必须指出一些东西才能查看test.exs,或者它在运行mix test 时会自动知道要查找此文件吗? (我仍然无法显示我的记录器)
  • config.exs 文件具有import_config "#{Mix.env}.exs" 行并且运行mix testMIX_ENV=test mix test 时,它会预测test.exs 文件。
  • > 警告::logger 应用程序的:compile_time_purge_level 选项已弃用,请改用:compile_time_purge_matching。 Elixir 1.9.
猜你喜欢
  • 2020-12-05
  • 2020-10-10
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
相关资源
最近更新 更多