SlideShare a Scribd company logo
1 of 18
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

More Related Content

What's hot

JRuby and Google App Engine
JRuby and Google App EngineJRuby and Google App Engine
JRuby and Google App Enginejoshsmoore
 
Introduction to Apache Maven
Introduction to Apache MavenIntroduction to Apache Maven
Introduction to Apache MavenRajind Ruparathna
 
Performance tips for Symfony2 & PHP
Performance tips for Symfony2 & PHPPerformance tips for Symfony2 & PHP
Performance tips for Symfony2 & PHPMax Romanovsky
 
An Introduction to Maven Part 1
An Introduction to Maven Part 1An Introduction to Maven Part 1
An Introduction to Maven Part 1MD Sayem Ahmed
 
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 environmentMichelantonio Trizio
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1LiviaLiaoFontech
 
Polygot Java EE on the GraalVM
Polygot Java EE on the GraalVMPolygot Java EE on the GraalVM
Polygot Java EE on the GraalVMRyan Cuprak
 
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 baseRalf Sternberg
 
Angular + JHipster - JHipster Conf
Angular + JHipster - JHipster ConfAngular + JHipster - JHipster Conf
Angular + JHipster - JHipster ConfWilliam Marques
 
Java 8 in Anger (JavaOne)
Java 8 in Anger (JavaOne)Java 8 in Anger (JavaOne)
Java 8 in Anger (JavaOne)Trisha Gee
 

What's hot (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)
 

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

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 rubyDeepak Chandella
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on railspmashchak
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorialsunniboy
 
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 2013Brian Sam-Bodden
 
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...Katy Slemon
 
RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and RailsAlan Hecht
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Railsanides
 
Viridians on Rails
Viridians on RailsViridians on Rails
Viridians on RailsViridians
 
Integration and Acceptance Testing
Integration and Acceptance TestingIntegration and Acceptance Testing
Integration and Acceptance TestingAlan Hecht
 
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 FrameworkEdureka!
 
All girlhacknight intro to rails
All girlhacknight intro to railsAll girlhacknight intro to rails
All girlhacknight intro to railsNola Stowe
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Railsmithunsasidharan
 
Onion Architecture with S#arp
Onion Architecture with S#arpOnion Architecture with S#arp
Onion Architecture with S#arpGary Pedretti
 

Similar to 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
 

Recently uploaded

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Recently uploaded (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

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.