:aggregated_failures标志如何在示例具有执行结果和状态后进行钩挂

时间:2019-03-26 20:01:15

标签: ruby rspec

我正在维护用Rspec&Capybara和SitePrism(无Rails)编写的独立测试自动化套件。

最近我开始将其与Testrail集成以进行报告-我为此使用了rspec-testrail gem,但是我不得不对其进行一些修改,因为我还想发送带有测试进度和报告的Slack通知。

无论如何,集成都可以顺利进行,除了示例处理依赖于具有异常且没有异常的示例会导致将测试状态设置为“通过Testrail”这一事实。 看起来,after :each中的after :exampleRspec.configure都不能保证示例已完成运行。 我也按照here的描述尝试过around :examplearound :each,但无济于事。

我检查了example.metadata的内容,发现example.metadata[:execution_result]仅具有started_at变量,但完成的示例还将具有finished_atstatus变量,根据{{​​3}}

我的怀疑(在阅读了津津乐道的文档之后)是:aggregated_failures导致元数据结构不同的原因,并且多个期望在线程中运行,这些线程后来合并为一个回溯。

您知道我如何等待示例完成或如何进入示例完成状态吗? 或者,也许我应该创建一个自定义格式化程序,在将示例通知打印到控制台之后挂在这里(我想将stacktrace保留在那里)。

我的代码如下:

测试(两个断言均失败):

require 'spec_helper'

feature 'Sign in' do
  let(:login_page) { LoginPage.new }
  let(:user) { { email: ENV['ADMIN_EMAIL'], password: ENV['ADMIN_PASSWORD'] } }

  scenario 'is successful and user is redirected to dashboard for user with correct credentials', testrail_id: 5694 do
    login_page.load
    login_page.form.email.set(user[:email])
    login_page.form.password.set(user[:password])
    login_page.form.submit_btn.click
    expect(login_page.sidebar).to have_jobs(text: "some_nonexistenttext")
    login_page.logout
    expect(current_url).to have_content "google.com"
  end
end

上述测试的控制台输出:

Failures:

  1) Sign in is successful and user is redirected to dashboard for user with correct credentials
     Got 2 failures:

     1.1) Failure/Error: expect(login_page.sidebar).to have_jobs(text: "blala")
            expected #has_jobs?({:text=>"some_nonexistenttext"}) to return true, got false
          # ./spec/auth/login_spec.rb:13:in `block (3 levels) in <top (required)>'

     1.2) Failure/Error: expect(current_url).to have_content "google.com"
            expected to find text "google.com" in "https://example.com/"
          # ./spec/auth/login_spec.rb:15:in `block (3 levels) in <top (required)>'

Finished in 53.91 seconds (files took 1.45 seconds to load)
4 examples, 1 failure

Failed examples:

rspec ./spec/auth/login_spec.rb:8 # Sign in is successful and user is redirected to dashboard for user with correct credentials

规范助手:

require 'rubygems'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'site_prism'
require 'slack-notifier'

require_relative '../config'
require_relative '../lib/testrail'

...

RSpec.configure do |config|
  config.define_derived_metadata do |meta|
    meta[:aggregate_failures] = true
  end
  config.example_status_persistence_file_path = 'examples.txt'
  config.before :all do
    testrail_initialize_test_run!
  end

  config.after :example, testrail_id: proc { |value| !value.nil? } do |example|
    RSpec::Testrail.process(example)
  end
end

处理方法(to the docs

  def process(example)
    if example.exception
      status = 5
      message = example.exception.message
      slack_notifier.publish(message_text "Failed")
    elsif example.skipped? || example.pending?
      puts 'Example skipped or pending'
      status = 10
      message = 'Pending or not implemented'
    else
      status = 1
      message = ''
    end
    client.send_post("add_result_for_case/#{testrun['id']}/#{example.metadata[:testrail_id]}",
                      status_id: status,
                      comment: message)
  end

1 个答案:

答案 0 :(得分:0)

所以基本上我只需要使用一个记者监听器并处理其中的通知即可:)

trdObj.getAccount().getSubAccounts.stream()
        .max(Comparator.comparing(SubAccount::getSubAccountId))
        .map(SubAccount::getSubAccountId)
        .ifPresent(maxId -> {
            // ...
        });