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. With an error message is found in a log file, Cucumber still indicated the step passed. 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)). 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.
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 :
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. You can't use the block of code you posted. You'll start writing RSpec tests. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.