Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...
Cucumber testing
1. Cucumber Testing Installation
1. Add the gems dependencies in your application’s Gemfile, for group :test only:
Gemfile
1 group :test do
2
3 # RSpec
4 gem 'rspec'
gem 'rspec-rails'
5
6
# Cucumber
7
gem 'cucumber-rails', :require => false
8
gem 'database_cleaner'
9
10
end
11
2. Run the bundle command to install the gems:
bundle install
3. If this is the first time you run tests on your application, don’t forget to
first prepare your test database:
rake db:migrate
rake db:test:load
4. Those frameworks need little configuration files. They can be generated with
the following commands:
2. > rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
and
> rails generate cucumber:install
create config/cucumber.yml
create script/cucumber
chmod script/cucumber
create features/step_definitions
create features/support
create features/support/env.rb
exist lib/tasks
create lib/tasks/cucumber.rake
gsub config/database.yml
gsub config/database.yml
force config/database.yml
5. Finally you can write a nice test. This tutorial will make use of Cucumber to
write and run tests.
Therefore I define my test in my own language in a features file:
features/test_cases/simple.feature
1 Feature: Simple testing
2
3 Scenario: Arriving on home page gives a 200 response code
4 When I visit the page "/"
Then the response code should be "200"
5
4. > cucumber
Using the default profile...
Feature: Simple testing
Scenario: Arriving on home page gives a 200 response code # featurestest_casessimple.feature:3
When I visit the page "/" #
featurestest_casessimple.feature:4
Undefined step: "I visit the page "/"" (Cucumber::Undefined)
featurestest_casessimple.feature:4:in `When I visit the page "/"'
Then the response code should be "200" # featurestest_casessimple.feature:5
Undefined step: "the response code should be "200"" (Cucumber::Undefined)
featurestest_casessimple.feature:5:in `Then the response code should be "200"'
1 scenario (1 undefined)
2 steps (2 undefined)
0m0.020s
You can implement step definitions for undefined steps with these snippets:
When /^I visit the page "(.*?)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
Then /^the response code should be "(.*?)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
5. You can see a colored output (on Windows, you will need to use Ansicon to see
the colors), much more sexier than usual Test::Unit output.
Currently our test is failing, as Cucumber does not know yet what to do with our
language. As you can see at the end of the output, it suggests a nice code
snippet with a regular expression to implement the missing definition.
This is a real time saver: all you have to do is copy/paste this code snippet to
your definition file, and implement it.
7. So next step is to implement the missing definition, done in a definitions
file:
features/step_definitions/main_steps.rb
When /^I visit the page "(.*?)"$/ do |iPagePath|
visit iPagePath
end
Then /^the response code should be "(.*?)"$/ do |iResponseCode|
page.status_code.should == iResponseCode.to_i
end
Now you can re-run your cucumber test suite and admire the result:
6. > cucumber
Using the default profile...
Feature: Simple testing
Scenario: Arriving on home page gives a 200 response code # featurestest_casessimple.feature:3
When I visit the page "/" # features/step_definitions/main_steps.rb:1
Then the response code should be "200" # features/step_definitions/main_steps.rb:5
1 scenario (1 passed)
2 steps (2 passed)
0m2.551s
With these you can already write a great test suite, very easy to maintain with
reusable and understandable sentences in your tests. Developers working after
you on your project will thank you!