有没有办法在Cucumber After hook中向异常消息添加内容?

有没有办法在Cucumber After hook中向异常消息添加内容?,cucumber,Cucumber,这将输出异常消息: After {|scenario| p scenario.exception.message} 我想在这里添加一些文字,比如: After {|scenario| scenario.exception.message << "extra data"} require 'cucumber/formatter/junit' module Cucumber::Formatter class Custom < Junit def format_ex

这将输出异常消息:

After {|scenario| p scenario.exception.message}
我想在这里添加一些文字,比如:

After {|scenario| scenario.exception.message << "extra data"}
require 'cucumber/formatter/junit'

module Cucumber::Formatter
  class Custom < Junit
    def format_exception(exception)
      (["extra data"] + ["#{exception.message} (#{exception.class})"] + exception.backtrace).join("\n")
    end
  end
end
在{| scenario | scenario.exception.message之后如果您编写:

After do |scenario|
  p scenario.exception.message
end
在控制台中运行Cucumber,您将看到它在执行After hook之前将自己的异常消息打印到控制台

因此,据我所知,您不能更改Cucumber的异常消息,因为它已经打印到输出中

作为一种解决方法,您可以提出自己的异常:

After do |scenario|
  raise 'my message'
end

它将出现在Cucumber的输出中

看起来这样做的方法是创建自定义Cucumber格式化程序。如下所示:

After {|scenario| scenario.exception.message << "extra data"}
require 'cucumber/formatter/junit'

module Cucumber::Formatter
  class Custom < Junit
    def format_exception(exception)
      (["extra data"] + ["#{exception.message} (#{exception.class})"] + exception.backtrace).join("\n")
    end
  end
end

我想你可以在中得到更详细的答案。我想他们会问你为什么需要这个,所以你最好在他们提问之前写出来。)谢谢。我已经在cucumber IRC频道提问,并在那里发布了这个问题的链接。我会用更多的上下文更新这个问题,并将链接发送到cucumber google group。