如何在黄瓜中标记步骤失败?

时间:2013-01-29 19:32:12

标签: ruby cucumber

我正在检查日志文件中是否有任何错误消息。如果在日志文件中找到错误消息,那么我使用'raise'语句打印出创建并继续检查下一个日志文件。如果在日志文件中找到错误消息,Cucumber仍然表示该步骤已通过。我想知道如何标记步骤失败,因此它会在运行结束时打印它(例如:2个场景(1个失败,1个传递) 4个步骤(1个失败,3个过去))。任何帮助将不胜感激!

                 Scenario: Running rake test
                   Given the system is installed
                   Then I run the rake test



        logs_all = s.sudo "egrep -i '#{error_message}' #{log_file}"
        logs_all.each do |hostname, logs|
           unless logs.empty?
            puts line, "Unhappy logs on #{hostname}", line, logs
            happy = false
           end

          begin
            raise "Unhappy logs found! in #{log_file}" unless happy
          rescue StandardError => error
            puts error.message
          end

        end

1 个答案:

答案 0 :(得分:-1)

首先,我建议阅读http://pragprog.com/book/hwcuc/the-cucumber-book或至少http://pragprog.com/book/achbd/the-rspec-book,其中包含Cuccumber的全部内容。

黄瓜测试必须通过,而不是失败。如果一个失败,则可以跳过另一个,这不是我们在运行测试套件时想要的。但是,如果日志不为空,您可以检查步骤是否会引发错误。

场景可能是:

Feature: Logs
Scenario: Checking non-empty log files
    Given there are logged errors
    When I check if logs are empty
    Then an error should be raised

运行cucumber,它会建议必要的步骤:

You can implement step definitions for undefined steps with these snippets:

Given /^there are logged errors$/ do
  pending # express the regexp above with the code you wish you had
end

When /^I check if logs are empty$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^an error should be raised$/ do
  pending # express the regexp above with the code you wish you had
end

将这些文件放入... / features / step_definitions / xxx_steps.rb文件中,然后开始用实际代码替换pending。您不能使用您发布的代码块。你将开始编写RSpec测试。第一步可能是编写一个读取日志的方法。第二种方法检测是否有任何日志不为空。最后一个规范将检查是否出现错误。

HTH