有没有什么好的方法可以把Cucumber/Watir/Ruby的测试结果写入数据库?

Is there a good way to write Cucumber/Watir/Ruby test results to a database?

我正在使用 Cucumber/Watir/Ruby 堆栈和 PageObjects。 我想将所有测试结果存储在数据库中,而不是像 pretyface gem 那样制作漂亮的报告。 有人做过吗?

谢谢

您可以为数据库连接定义 hook to execute after each scenario writing to the database. I have worked with sequel gem,但还有很多其他连接。

After do |scenario|
 begin
   //Get values for the scenario
   scenario.name 
   scenario.failed?
 //  <database code to add values>
 end
end

所以有两种方法可以尝试。 1 是编写自己的格式化程序。我还没有尝试过,但这将是我的下一个副项目。 第二种方法是从 After Hook 部分的场景对象中破解所有你关心的字段。 我已经为黄瓜的 Pre-2.0 版本完成了此操作。当我查看 Post-2.0 时,对象似乎没有公开我想要的所有数据,我想这是 Pretyface gem 仍未更新到 1.9 之后的部分原因。 3个左右。 下面的代码一点也不漂亮,但它确实有效。只需从 After Hooks 方法中调用它

def read_and_export_scenario_results(scenario)
    set1 = scenario.instance_variable_get(:@current_visitor)

    feature_loc = scenario.feature.location
    feature_name = scenario.feature.title
    feature_desc = scenario.feature.description
    scenario_name = scenario.title
    profile = set1.configuration.options.send(:profiles)[0]
    #get data from Listeners

    results_loc = set1.configuration.options.send(:expanded_args)[set1.configuration.options.send(:expanded_args).size-1]
    run = results_loc.split('/')[2]

    steps = Array.new(set1.runtime.results.steps.count)
    i=0
    set1.runtime.results.steps.each do |step|
      step_hash = Hash.new
      step_hash['StepName'] = step.name
      step_hash['ReportedException'] = step.reported_exception
      step_hash['Status'] = step.status
      step_hash['Message'] = step.multiline_arg
      steps[i] = step_hash
      i += 1
    end

  if scenario.failed?
    pic_dir = 'screenshots'
    pic_name = "ERR_#{run}_#{@current_page.class}_#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}.png"
    pic_src = "#{pic_dir}\#{pic_name}"

    unless File.directory?(pic_dir)
      Dir.mkdir(pic_dir)
    end

    @current_page.save_screenshot(pic_src)
    embed(pic_src, 'image/png')
  else
    pic_src = ''
  end
  sql_object = Sql.new
  sql_load_scenario = "AddScenario '#{feature_loc}', '#{feature_name}', '#{feature_desc}', '#{scenario_name}', '#{profile}', '#{run}', '#{pic_src}', '#{results_loc}', '#{FigNewton.application}', '#{FigNewton.base_location}'"
  feature_ID = sql_object.execute_query(sql_load_scenario).to_a[0]
  steps.each do |step|
    sql_load_steps = "AddScenarioStep #{feature_ID}, '#{step['StepName']}', '#{step['Status']}', '#{step['ReportedException']}', '#{step['Message']}'"
    sql_object.execute_query(sql_load_steps)
end