SlideShare une entreprise Scribd logo
Behavioural Testing Ruby/Rails Apps @
Scale - Rspec & Cucumber
Testing & Automation Series
Topics
1. Overview of Testing
Unit Test, Integration Test, Acceptance Test/functional Test
2. TDD and BDD approach
3. Rspec
4. Shoulda-Matchers
5. Factory girl
6. Cucumber
7. Gherkin
8. Capybara
9. Webdriver
1. Unit Test
A unit test focuses on a single “unit of code” – usually a Method or function in an object or module
2. Integration Test
Multiple pieces are tested together eg: Testing the user creation flow from controller to model
3. Acceptance test/ Function Test/Automation Test
Automatic testing of the entire application End to End Testing or "Full Application", for example using
a tool like Selenium to automatically run a browser.
Overview of Testing
1.TDD - Test Driven Development
TDD focuses on the behaviour of an object
a. Write Test and see test fail
b. Write code
c. Run Test
d. Clean up code
Red, Green, Refactor
2. BDD - Behaviour Driven Development
BDD focuses on the behaviour of the application
a. Write code
b. Write Test
c. Run Test
d. Clean up code
TDD and BDD
a. BDD focuses on the behavioural aspect of the system rather than the implementation
aspect of the system that TDD focuses on
b. BDD uses a more verbose style so that it can be read almost like a sentence.
c. BDD gives a clearer understanding as to what the system should do from the
perspective of the developer and the customer. TDD only gives the developer an
understanding of what the system should do.
Difference Between TDD and BDD
Rspec
1. Rspec is a Testing Framework that follows the Behaviour driven development
approach(BDD) – Also It can be TDD
2. Rspec stands for - Ruby Specification
3. Rspec is a low level testing, Rspec is very good for unit testing, that is testing models,
controllers, views.
Add below code into your gem file and bundle install or install it by command line
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
Or gem install rspec-rails
Create the Basic Skelton
rails generate rspec:install
Create the RSpec binstub. In short, the binstub will allow you to run RSpec with bin/rspec instead of
bundle exec rspec
bundle binstubs rspec-core
Installation and syntax
Rspec syntax
Require spec helper
require 'spec_helper'
Describe block - With RSpec, we are always describing the behaviour of classes, modules and their
methods. The describe block is always used at the top to put specs in a context. It can accept either
a class name, in which case the class needs to exist, or any string you'd like.
Context block
It block
https://relishapp.com/rspec/rspec-expectations/v/3-1/docs/built-in-matchers/equality-matchers
Rspec Example
require 'spec_helper'
describe User do
describe "firstname validation" do
context "firstname is present" do
before(:each) do
@user = User.new(firstname: "satheesh")
end
it "does not add an error on the 'firstname' attribute" do
@user.valid?
expect(@user.errors[:firstname].size).to eq(0)
end
end
end
end
shoulda-matchers lets us spec common Rails functionality, like validations and associations, with less
code.
gem 'shoulda-matchers'
bundle install
should validate_presence_of(:firstname)
http://matchers.shoulda.io/docs/v3.1.1/
shoulda-matchers
Factory-Girl
factory_girl is a fixtures replacement with a straightforward definition syntax(i.e Test data)
gem 'factory_girl' and bundle install
Create the directory called factories and define with the filename same as table name
FactoryGirl.define do
factory :user do
firstname "satheesh"
lastname "kumar"
email "satheeshkumark@expertus.com"
password "password"
end
end
@user = FactoryGirl.build(:user)
Cucumber is a high-level testing framework which is designed to let you use Behaviour
Driven Development (BDD) to create Ruby on Rails applications
lCucumber’s unique feature is that it uses English (or a number of other supported
languages) to define an application’s behaviour.
lGherkin is the language that Cucumber understands
lhttps://github.com/cucumber/cucumber/wiki/Gherkin
It is a Business Readable, Domain Specific Language that lets you describe software’s
behaviour. It uses the detailing how that behaviour is implemented
Feature file consists of the scenario
Step definition consists of the ruby code to pass the scenario
Cucumber
Capybara
Capybara helps you test web applications by simulating how a real user would
interact with your app.
It is agnostic about the driver running your tests and comes with Rack::Test and
Selenium support built in.
Web Kit is supported through an external gem.
No setup necessary for Rails and Rack application. Works out of the box.
Switch the backend your tests run against from fast headless mode to an actual
browser with no changes to your tests.
Powerful synchronization features mean you never have to manually wait for
asynchronous processes to complete.
http://www.rubydoc.info/gems/capybara/2.6.2
Run
'gem install cucumber-rails'
'gem install capybara'
Login Feature:
Feature: Login page
Scenario: User signs in successfully with email and password
Given I am on the login page
When I am a new, authenticated user
Then I should be logged in
Installation and Example
Example:
Given(/^I am on the login page$/) do
visit "https://www.satheesh.devtalent01.exphosted.com"
end
When(/^I am a new, authenticated user$/) do
@user = FactoryGirl.build(:user)
fill_in "user_email", :with => @user.email
fill_in "user_password", :with => @user.password
click_button "Sign In"
end
Then(/^I should be logged in$/) do
expect(find('#current_tasks')).to have_content('IN PROGRESS')
end
Installation and Example
Web Drivers
Capybara uses the same DSL to drive a variety of browser and headless drivers
Rack Test
Rack Test is Capybara's default driver.
It is written in pure Ruby and does not have any support for executing JavaScript.
Since the Rack Test driver interacts directly with Rack interfaces, it does not require a
server to be started
If your application is not a Rack application (Rails, Sinatra and most other Ruby
frameworks are Rack applications) then you cannot use this driver
you cannot use the Rack Test driver to test a remote application, or to access remote URLs
(e.g., redirects to external sites, external APIs, or OAuth services)
Web Drivers
Raketest - By default, Capybara uses the rake test which is fast but limited: it does not support
JavaScript, nor is it able to access HTTP resources outside of your Rack application, such as
remote APIs and OAuth services
Selenium - By default, JavaScript tests are run using the :selenium driver
Capybara-webkit - The capybara-webkit driver is for true headless testing. It uses QtWebKit to
start a rendering engine process. It can execute JavaScript as well. It is significantly faster than
drivers like Selenium since it does not load an entire browser
Poltergeist - Poltergeist is another headless driver which integrates Capybara with PhantomJS. It
is truly headless, so doesn't require Xvfb to run on your CI server. It will also detect and report
any Javascript errors that happen within the page.
THANK YOU

Contenu connexe

Tendances

Tendances (20)

JRuby and Google App Engine
JRuby and Google App EngineJRuby and Google App Engine
JRuby and Google App Engine
 
Phalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil ConferencePhalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil Conference
 
Introduction to Apache Maven
Introduction to Apache MavenIntroduction to Apache Maven
Introduction to Apache Maven
 
Performance tips for Symfony2 & PHP
Performance tips for Symfony2 & PHPPerformance tips for Symfony2 & PHP
Performance tips for Symfony2 & PHP
 
An Introduction to Maven Part 1
An Introduction to Maven Part 1An Introduction to Maven Part 1
An Introduction to Maven Part 1
 
How to deploy a Java application on Google App engine Flexible environment
How to deploy a Java application on Google App engine Flexible environmentHow to deploy a Java application on Google App engine Flexible environment
How to deploy a Java application on Google App engine Flexible environment
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1
 
Introduction to Maven
Introduction to MavenIntroduction to Maven
Introduction to Maven
 
Os Bubna
Os BubnaOs Bubna
Os Bubna
 
Maven tutorial for beginners
Maven tutorial for beginnersMaven tutorial for beginners
Maven tutorial for beginners
 
TorqueBox
TorqueBoxTorqueBox
TorqueBox
 
Maven Overview
Maven OverviewMaven Overview
Maven Overview
 
Polygot Java EE on the GraalVM
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVM
 
Introduction to Maven
Introduction to MavenIntroduction to Maven
Introduction to Maven
 
java new technology
java new technologyjava new technology
java new technology
 
Apache Maven In 10 Slides
Apache Maven In 10 SlidesApache Maven In 10 Slides
Apache Maven In 10 Slides
 
Single Sourcing RAP and RCP - Desktop and web clients from a single code base
Single Sourcing RAP and RCP - Desktop and web clients from a single code baseSingle Sourcing RAP and RCP - Desktop and web clients from a single code base
Single Sourcing RAP and RCP - Desktop and web clients from a single code base
 
Angular + JHipster - JHipster Conf
Angular + JHipster - JHipster ConfAngular + JHipster - JHipster Conf
Angular + JHipster - JHipster Conf
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
 
Java 8 in Anger (JavaOne)
Java 8 in Anger (JavaOne)Java 8 in Anger (JavaOne)
Java 8 in Anger (JavaOne)
 

Similaire à Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber

Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
pmashchak
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
sunniboy
 

Similaire à Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber (20)

Capybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using rubyCapybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using ruby
 
Basic RSpec 2
Basic RSpec 2Basic RSpec 2
Basic RSpec 2
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 
How to implement ruby on rails testing practices to build a successful web ap...
How to implement ruby on rails testing practices to build a successful web ap...How to implement ruby on rails testing practices to build a successful web ap...
How to implement ruby on rails testing practices to build a successful web ap...
 
RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and Rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
rails.html
rails.htmlrails.html
rails.html
 
rails.html
rails.htmlrails.html
rails.html
 
Viridians on Rails
Viridians on RailsViridians on Rails
Viridians on Rails
 
Integration and Acceptance Testing
Integration and Acceptance TestingIntegration and Acceptance Testing
Integration and Acceptance Testing
 
Building Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkBuilding Application with Ruby On Rails Framework
Building Application with Ruby On Rails Framework
 
Rspec
RspecRspec
Rspec
 
RoR guide_p1
RoR guide_p1RoR guide_p1
RoR guide_p1
 
All girlhacknight intro to rails
All girlhacknight intro to railsAll girlhacknight intro to rails
All girlhacknight intro to rails
 
Dev streams2
Dev streams2Dev streams2
Dev streams2
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Onion Architecture with S#arp
Onion Architecture with S#arpOnion Architecture with S#arp
Onion Architecture with S#arp
 

Dernier

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Dernier (20)

Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Transforming The New York Times: Empowering Evolution through UX
Transforming The New York Times: Empowering Evolution through UXTransforming The New York Times: Empowering Evolution through UX
Transforming The New York Times: Empowering Evolution through UX
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Server-Driven User Interface (SDUI) at Priceline
Server-Driven User Interface (SDUI) at PricelineServer-Driven User Interface (SDUI) at Priceline
Server-Driven User Interface (SDUI) at Priceline
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Motion for AI: Creating Empathy in Technology
Motion for AI: Creating Empathy in TechnologyMotion for AI: Creating Empathy in Technology
Motion for AI: Creating Empathy in Technology
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 

Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber

  • 1. Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber Testing & Automation Series
  • 2. Topics 1. Overview of Testing Unit Test, Integration Test, Acceptance Test/functional Test 2. TDD and BDD approach 3. Rspec 4. Shoulda-Matchers 5. Factory girl 6. Cucumber 7. Gherkin 8. Capybara 9. Webdriver
  • 3. 1. Unit Test A unit test focuses on a single “unit of code” – usually a Method or function in an object or module 2. Integration Test Multiple pieces are tested together eg: Testing the user creation flow from controller to model 3. Acceptance test/ Function Test/Automation Test Automatic testing of the entire application End to End Testing or "Full Application", for example using a tool like Selenium to automatically run a browser. Overview of Testing
  • 4. 1.TDD - Test Driven Development TDD focuses on the behaviour of an object a. Write Test and see test fail b. Write code c. Run Test d. Clean up code Red, Green, Refactor 2. BDD - Behaviour Driven Development BDD focuses on the behaviour of the application a. Write code b. Write Test c. Run Test d. Clean up code TDD and BDD
  • 5. a. BDD focuses on the behavioural aspect of the system rather than the implementation aspect of the system that TDD focuses on b. BDD uses a more verbose style so that it can be read almost like a sentence. c. BDD gives a clearer understanding as to what the system should do from the perspective of the developer and the customer. TDD only gives the developer an understanding of what the system should do. Difference Between TDD and BDD
  • 6. Rspec 1. Rspec is a Testing Framework that follows the Behaviour driven development approach(BDD) – Also It can be TDD 2. Rspec stands for - Ruby Specification 3. Rspec is a low level testing, Rspec is very good for unit testing, that is testing models, controllers, views.
  • 7. Add below code into your gem file and bundle install or install it by command line group :development, :test do gem 'rspec-rails', '~> 2.0' end Or gem install rspec-rails Create the Basic Skelton rails generate rspec:install Create the RSpec binstub. In short, the binstub will allow you to run RSpec with bin/rspec instead of bundle exec rspec bundle binstubs rspec-core Installation and syntax
  • 8. Rspec syntax Require spec helper require 'spec_helper' Describe block - With RSpec, we are always describing the behaviour of classes, modules and their methods. The describe block is always used at the top to put specs in a context. It can accept either a class name, in which case the class needs to exist, or any string you'd like. Context block It block https://relishapp.com/rspec/rspec-expectations/v/3-1/docs/built-in-matchers/equality-matchers
  • 9. Rspec Example require 'spec_helper' describe User do describe "firstname validation" do context "firstname is present" do before(:each) do @user = User.new(firstname: "satheesh") end it "does not add an error on the 'firstname' attribute" do @user.valid? expect(@user.errors[:firstname].size).to eq(0) end end end end
  • 10. shoulda-matchers lets us spec common Rails functionality, like validations and associations, with less code. gem 'shoulda-matchers' bundle install should validate_presence_of(:firstname) http://matchers.shoulda.io/docs/v3.1.1/ shoulda-matchers
  • 11. Factory-Girl factory_girl is a fixtures replacement with a straightforward definition syntax(i.e Test data) gem 'factory_girl' and bundle install Create the directory called factories and define with the filename same as table name FactoryGirl.define do factory :user do firstname "satheesh" lastname "kumar" email "satheeshkumark@expertus.com" password "password" end end @user = FactoryGirl.build(:user)
  • 12. Cucumber is a high-level testing framework which is designed to let you use Behaviour Driven Development (BDD) to create Ruby on Rails applications lCucumber’s unique feature is that it uses English (or a number of other supported languages) to define an application’s behaviour. lGherkin is the language that Cucumber understands lhttps://github.com/cucumber/cucumber/wiki/Gherkin It is a Business Readable, Domain Specific Language that lets you describe software’s behaviour. It uses the detailing how that behaviour is implemented Feature file consists of the scenario Step definition consists of the ruby code to pass the scenario Cucumber
  • 13. Capybara Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in. Web Kit is supported through an external gem. No setup necessary for Rails and Rack application. Works out of the box. Switch the backend your tests run against from fast headless mode to an actual browser with no changes to your tests. Powerful synchronization features mean you never have to manually wait for asynchronous processes to complete. http://www.rubydoc.info/gems/capybara/2.6.2
  • 14. Run 'gem install cucumber-rails' 'gem install capybara' Login Feature: Feature: Login page Scenario: User signs in successfully with email and password Given I am on the login page When I am a new, authenticated user Then I should be logged in Installation and Example
  • 15. Example: Given(/^I am on the login page$/) do visit "https://www.satheesh.devtalent01.exphosted.com" end When(/^I am a new, authenticated user$/) do @user = FactoryGirl.build(:user) fill_in "user_email", :with => @user.email fill_in "user_password", :with => @user.password click_button "Sign In" end Then(/^I should be logged in$/) do expect(find('#current_tasks')).to have_content('IN PROGRESS') end Installation and Example
  • 16. Web Drivers Capybara uses the same DSL to drive a variety of browser and headless drivers Rack Test Rack Test is Capybara's default driver. It is written in pure Ruby and does not have any support for executing JavaScript. Since the Rack Test driver interacts directly with Rack interfaces, it does not require a server to be started If your application is not a Rack application (Rails, Sinatra and most other Ruby frameworks are Rack applications) then you cannot use this driver you cannot use the Rack Test driver to test a remote application, or to access remote URLs (e.g., redirects to external sites, external APIs, or OAuth services)
  • 17. Web Drivers Raketest - By default, Capybara uses the rake test which is fast but limited: it does not support JavaScript, nor is it able to access HTTP resources outside of your Rack application, such as remote APIs and OAuth services Selenium - By default, JavaScript tests are run using the :selenium driver Capybara-webkit - The capybara-webkit driver is for true headless testing. It uses QtWebKit to start a rendering engine process. It can execute JavaScript as well. It is significantly faster than drivers like Selenium since it does not load an entire browser Poltergeist - Poltergeist is another headless driver which integrates Capybara with PhantomJS. It is truly headless, so doesn't require Xvfb to run on your CI server. It will also detect and report any Javascript errors that happen within the page.