rspec is a full-featured testing framework that will let you write what Rails considers unit tests, functional tests, and integration tests. All of these exercise Ruby code through various layers of your Rails application. All of these tests simulate requests to your Rails application, but don't actually run the application end-to-end over the network.
cucumber is a browser based integration testing framework, which allows writing automated tests that run against the entire Rails application accessed from within an automated web browser. This allows you to write automated tests about in-browser behavior for JS or CSS. Cucumber provides a unique angle on integration testing that uses plain english specification mapped to code via regular expressions. This allows a more natural "Behavior Driven Development" model - describing what a web application should do, in plain language, from the perspective of the user.
capybara is a particular web driver powering the cucumber integration testing framework, that uses headless webkit. This allows running a headless (without UI) Chrome/Webkit browser for automated testing. This is very useful both in development, but especially on a remote test/continuous integration server.
So rspec and cucumber are similar in being testing frameworks with their own way of specifying things. rspec has a nice DSL that's very readable while being actual code. cucumber maps plain text descriptions to real code.
Though cucumber is usually used on top of capybara, you can also use rspec to drive capybara integration tests. The tests are written in either rspec or cucumber, but capybara is an integration engine underneath.
RSpec and Cucumber are both testing frameworks. RSpec includes traditional Unit Testing (which means testing a class or part of the application in isolation from the rest of the application. So your model does what your model is supposed to do, the controller does what it is supposed to do, etc).
RSpec and Cucumber both are used for Acceptance Testing (Which is called ATDD, BDD, Specification by Example, etc depending on who you ask). These are business-case driven Integration Tests, which mean they simulate the way a user uses the application and uses the full Rails stack so problems with the way the different parts of your application work together can be found in a way that unit testing will not find.
The main difference between RSpec and Cucumber are the business readability factor. Cucumber's main draw is that the specification (features) are separate from the test code, so your product owners can provide or review the specification without having to dig through code. These are the .feature files that you make in Cucumber. RSpec has a similar mechanism, but instead you describe a step with a Describe, Context or It block that contains the business specification, and then immediately have the code that executes that statement. This approach is a little easier for developers to work with but a little harder for non-technical folks.
Which to use? If you are the sole developer and product owner, then I would stick with RSpec, I feel it's easier to a technical person to understand, offers a few advantages in keeping things scoped and under control, and keep you out of messing with RegExs for test steps. If you are building this for a client, and they are hands-on with regard to the Specification, go with Cucumber for your Acceptance Test and use RSpec for Unit Tests.
Just to demonstrate the main difference between the two:
Cucumber:
#articles.feature
Given an article exists called "Testing Demonstration"
When I visit the list of articles
Then I should see an article called "Testing Demonstration"
#article_steps.rb
Given /^an article exists called "(.+)"$/ do |title|
FactoryGirl.create(:article, title: title)
end
When /^I visit the list of articles$/ do
visit articles_path
end
Then /^I should see an article called "(.+)"$/ do |title|
page.should have_content title
end
Rspec
describe "Articles" do
let(:article) { FactoryGirl.create(:article) }
context "Index Page" do
before { visit articles_path }
it { page.should have_content article.title }
end
end
Capybara is a tool that interacts with a website the way a human would (like visiting a url, clicking a link, typing text into a form and submitting it). It is used to emulate a user's flow through a website. With Capybara you can write something like this:
describe "the signup process", :type => :feature do
before :each do
User.make(:email => 'user@example.com', :password => 'caplin')
end
it "signs me in" do
visit '/sessions/new'
within("#session") do
fill_in 'Login', :with => 'user@example.com'
fill_in 'Password', :with => 'password'
end
click_link 'Sign in'
page.should have_content 'Success'
end
end
Cucumber is a tool to write human-readable tests that are mapped into code. With it, you can rewrite the above example like this:
Scenario: Signup process
Given a user exists with email "user@example.com" and password "caplin"
When I try to login with "user@example.com" and "caplin"
Then I should be logged in successfully
The almost plain-text interpretation is useful to pass around non-developers but also need some code mapped into it to actually work (the step definitions).
Usually you will use Capybara if you're testing a website and use Cucumber if you need to share those tests with non-developers. These two conditions are independent so you can use one without the other or both or none.
PS: in the code snippet there is some RSpec as well. This is needed because Cucumber or Capybara by themselves cannot test something. They rely on RSpec, Test::Unit or minitest to do the actual "Pass or Fail" work.
cucumber is a browser based integration testing framework, which allows writing automated tests that run against the entire Rails application accessed from within an automated web browser. This allows you to write automated tests about in-browser behavior for JS or CSS. Cucumber provides a unique angle on integration testing that uses plain english specification mapped to code via regular expressions. This allows a more natural "Behavior Driven Development" model - describing what a web application should do, in plain language, from the perspective of the user.
capybara is a particular web driver powering the cucumber integration testing framework, that uses headless webkit. This allows running a headless (without UI) Chrome/Webkit browser for automated testing. This is very useful both in development, but especially on a remote test/continuous integration server.
So rspec and cucumber are similar in being testing frameworks with their own way of specifying things. rspec has a nice DSL that's very readable while being actual code. cucumber maps plain text descriptions to real code.
Though cucumber is usually used on top of capybara, you can also use rspec to drive capybara integration tests. The tests are written in either rspec or cucumber, but capybara is an integration engine underneath.
RSpec and Cucumber are both testing frameworks. RSpec includes traditional Unit Testing (which means testing a class or part of the application in isolation from the rest of the application. So your model does what your model is supposed to do, the controller does what it is supposed to do, etc).
RSpec and Cucumber both are used for Acceptance Testing (Which is called ATDD, BDD, Specification by Example, etc depending on who you ask). These are business-case driven Integration Tests, which mean they simulate the way a user uses the application and uses the full Rails stack so problems with the way the different parts of your application work together can be found in a way that unit testing will not find.
The main difference between RSpec and Cucumber are the business readability factor. Cucumber's main draw is that the specification (features) are separate from the test code, so your product owners can provide or review the specification without having to dig through code. These are the .feature files that you make in Cucumber. RSpec has a similar mechanism, but instead you describe a step with a Describe, Context or It block that contains the business specification, and then immediately have the code that executes that statement. This approach is a little easier for developers to work with but a little harder for non-technical folks.
Which to use? If you are the sole developer and product owner, then I would stick with RSpec, I feel it's easier to a technical person to understand, offers a few advantages in keeping things scoped and under control, and keep you out of messing with RegExs for test steps. If you are building this for a client, and they are hands-on with regard to the Specification, go with Cucumber for your Acceptance Test and use RSpec for Unit Tests.
Just to demonstrate the main difference between the two:
Cucumber:
#articles.feature
Given an article exists called "Testing Demonstration"
When I visit the list of articles
Then I should see an article called "Testing Demonstration"
#article_steps.rb
Given /^an article exists called "(.+)"$/ do |title|
FactoryGirl.create(:article, title: title)
end
When /^I visit the list of articles$/ do
visit articles_path
end
Then /^I should see an article called "(.+)"$/ do |title|
page.should have_content title
end
Rspec
describe "Articles" do
let(:article) { FactoryGirl.create(:article) }
context "Index Page" do
before { visit articles_path }
it { page.should have_content article.title }
end
end
Capybara is a tool that interacts with a website the way a human would (like visiting a url, clicking a link, typing text into a form and submitting it). It is used to emulate a user's flow through a website. With Capybara you can write something like this:
describe "the signup process", :type => :feature do
before :each do
User.make(:email => 'user@example.com', :password => 'caplin')
end
it "signs me in" do
visit '/sessions/new'
within("#session") do
fill_in 'Login', :with => 'user@example.com'
fill_in 'Password', :with => 'password'
end
click_link 'Sign in'
page.should have_content 'Success'
end
end
Cucumber is a tool to write human-readable tests that are mapped into code. With it, you can rewrite the above example like this:
Scenario: Signup process
Given a user exists with email "user@example.com" and password "caplin"
When I try to login with "user@example.com" and "caplin"
Then I should be logged in successfully
The almost plain-text interpretation is useful to pass around non-developers but also need some code mapped into it to actually work (the step definitions).
Usually you will use Capybara if you're testing a website and use Cucumber if you need to share those tests with non-developers. These two conditions are independent so you can use one without the other or both or none.
PS: in the code snippet there is some RSpec as well. This is needed because Cucumber or Capybara by themselves cannot test something. They rely on RSpec, Test::Unit or minitest to do the actual "Pass or Fail" work.
Nice post ! Thanks for sharing valuable information with us. Keep sharing..
ReplyDeleteRuby on Rails Online Course Bangalore
Thank you for providing useful content Ruby on Rails Online Training Bangalore
ReplyDeleteThanks for sharing the information.....It is very much useful for my research, keep sharing more articles
ReplyDeleteSelenium Training in Bangalore
Software Testing Training in Bangalore
Java Selenium Training in Bangalor
Best Selenium Training in Bangalore
Best Selenium Training Institute in Bangalore
Thanks for this lists
ReplyDeleteCucumber Training in Marathahalli
Cucumber Training in Bangalore
Java Selenium Automation Training in Bangalore
Selenium Training in Marathahalli
Manual testing training in bangalore
Software Testing Training in Bangalore
Selenium Software Training in Bangalore
Thanks for your blog!!
ReplyDeleteSelenium Automation Training in Bangalore
Selenium Training Institutes in Bangalore
Selenium Training in Marathahalli
Best Selenium Automation Training in Bangalore
Selenium Training in Marathahalli
Best Selenium Training in Bangalore
Selenium Software Training in Bangalore
Selenium Training Institutes in Bangalore
Top 10 selenium training institutes in bangalore