简体   繁体   English

如何标记黄瓜步骤失败?

[英]How to mark a step fail in Cucumber?

I'm checking to see if there is any error message in log files. 我正在检查日志文件中是否有任何错误消息。 If an error message found in a log file, then I use 'raise' statement to print out the founding and continue checking the next log file. 如果在日志文件中发现错误消息,那么我将使用“ raise”语句来打印出创建内容并继续检查下一个日志文件。 With an error message is found in a log file, Cucumber still indicated the step passed. 在日志文件中发现错误消息后,Cucumber仍指示已通过步骤。 I'd like to know how to mark a step fail, so it will print it at end of run (for example: 2 scenarios (1 failed, 1 passed) 4 steps (1 failed, 3 passed)). 我想知道如何标记一个步骤失败,因此它将在运行结束时将其打印出来(例如:2个场景(1个失败,1个通过)4个步骤(1个失败,3个通过))。 Any help would be appreciated! 任何帮助,将不胜感激!

                 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

First I would recommend reading http://pragprog.com/book/hwcuc/the-cucumber-book or at least http://pragprog.com/book/achbd/the-rspec-book which has a whole part on Cuccumber. 首先,我会建议你阅读http://pragprog.com/book/hwcuc/the-cucumber-book或至少http://pragprog.com/book/achbd/the-rspec-book这对Cuccumber一个整体部分。

Cucumber tests must pass, not fail. 黄瓜测试必须通过,不能失败。 If one fails, the other may be skipped, which is not what we want when running a test suite. 如果一个失败,则另一个可能会被跳过,这不是我们运行测试套件时想要的。 But you can check that a step raises an error if a log is not empty. 但是,如果日志不为空,则可以检查步骤是否引发错误。

A scenario could be : 一个方案可能是:

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

Run cucumber , it will suggest the necessary steps : 运行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

Put these into a .../features/step_definitions/xxx_steps.rb file, then start replacing pending with real code. 将它们放入... / features / step_definitions / xxx_steps.rb文件中,然后开始用实际代码替换pending You can't use the block of code you posted. 您不能使用您发布的代码块。 You'll start writing RSpec tests. 您将开始编写RSpec测试。 A first step could be writing a method which reads the logs. 第一步可能是编写一种读取日志的方法。 A second method detects if any log is not empty. 第二种方法检测是否有任何日志不为空。 The last spec will check that an error is raised. 最后一个规范将检查是否引发错误。

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM