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.