cucumber/ruby:可以输出“puts";到 --format html 文件?

2022-01-22 00:00:00 string-formatting ruby html cucumber

我有一些 ruby​​ 测试,它们调用不同的模块和类,它们详细说明了在执行过程中使用一些放置"命令所做的事情.

I've got some ruby tests that are calling different modules, classes where they detail what they're doing with some "puts" commands during execution.

如果您在控制台中运行这些测试,那么您将在控制台中看到puts"命令的输出,但如果您使用以下选项运行测试:

If you run those tests in the console then you will see the output of the "puts" command in the console but if you run the tests with the option:

ruby --format html --output file.html

那么所有这些信息都会丢失.有没有办法在 HTML 报告中记录简单的字符串消息?

then all that information is lost. Is there a way to log simple string messages inside the HTML report?

推荐答案

你可以记住每个场景的Before钩子中的World:

You can remember World in Before hook of each scenario:

# features/support/env.rb
Before do |scenario|
  $world = self
end

然后在您的外部支持类和模块中,您可以将 puts 用作:

Then in your support classes and modules outside world you can use puts as:

$world.puts 'something'

您还可以将 Cucumber 实例变量获取/设置为:

Also you can get/set Cucumber instance variables as:

$world.instance_variable_get(:@user)
$world.instance_variable_set(:@user, user)

我也更喜欢将这 2 种方法提取到 helpers 以获得更好的可见性:

I also prefer to extract those 2 methods to helpers for better visibility:

module Helpers
  def get_scenario_variable(symbol)
    $world.instance_variable_get(symbol)
  end

  def set_scenario_variable(symbol, value)
    $world.instance_variable_set(symbol, value)
  end
end

然后你可以在你需要这些方法的地方包含这个模块

Then you can include this module where you need those methods

相关文章