【问题标题】:How to add information to ExUnit's output for failing tests如何将信息添加到 ExUnit 的输出以应对失败的测试
【发布时间】:2019-12-31 21:17:52
【问题描述】:

我的测试的ExUnit.Callbacks.setup 函数会创建一个 ID,当测试失败时我想将其包含在 ExUnit 的输出中。有没有一种简单的方法可以做到这一点?我知道我可以编写一个自定义的 ExUnit.Formatter,但这似乎有点矫枉过正。

这个问题的背景是我正在使用 ExUnit 来帮助编写不是单元测试而是跨多个微服务的集成测试。 setup 函数生成的 ID 是跨微服务持续存在的 Spandex 跟踪 ID。当测试失败时,我想知道它的跟踪 ID 是什么,以便我可以 grep 查找该 ID 的所有微服务日志。

【问题讨论】:

    标签: elixir ex-unit


    【解决方案1】:

    一种方法是使用assert 和朋友的message 参数。该消息只会在断言失败时显示。

    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
    

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 2012-01-01
      • 2019-08-29
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      相关资源
      最近更新 更多