Tuesday 2 February 2016

Automation Testing Using Rspec and Cucumber In Ruby on Rails

Automation Testing:

1. Unit Test

A unit test focuses on a single “unit of code” – usually a function in an object or module. By making the test specific to a single function, the test should be simple, quick to write, and quick to run. This means you can have many unit tests, and more unit tests means more bugs caught. They are especially helpful if you need to change your code, as you can safely do so and trust that any other code will not break.

2. Integration Test

Multiple pieces are tested together, for example testing database access code against a test database

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.

TDD - Test Driven Development

a. Write Test
b. Write code
c. Run Test
d. Clean up code

Red, Green, Refactor

BDD - Behavior Driven Development

Difference Between TDD and BDD

BDD uses a more verbose style so that it can be read almost like a sentence. BDD is to test the TDD

  1. BDD focuses on the behavioural aspect of the system rather than the implementation aspect of the system that TDD focuses on.
  2. 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.
  3. BDD allows both the developer and the customer to work together to on requirements analysis that is contained within the source code of the system.

Webdriver

WebDriver is a tool for writing automated tests of websites. It aims to mimic the behaviour of a real user, and as such interacts with the HTML of the application.

Capybara uses the same DSL to drive a variety of browser and headless drivers
Rack_test
RackTest is Capybara's default driver.
It is written in pure Ruby and does not have any support for executing JavaScript.
Since the RackTest 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 RackTest driver to test a remote application, or to access remote URLs (e.g., redirects to external sites, external APIs, or OAuth services)

Selenium
Capybara-webkit
Poltergeist

Rspec

Rspec is a Testing Framework, Rspec stands for Ruby Specification. Rspec is a low level testing, Rspec is very good for unit testing, that is testing models, controllers, views.


Add below code into your gemfile 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

Cucumber 
Cucumber is a high-level testing framework which is designed to let you use Behaviour Driven Development (BDD) to create Ruby on Rails applications
Cucumber’s unique feature is that it uses English (or a number of other supported languages) to define an application’s behaviour.
Gherkin is the language that Cucumber understands
It is a Business Readable, Domain Specific Language that lets you describe software’s behaviour withIt Uses the struout detailing how that behaviour is implemented

Feature file consists of the scenario
Step definition consists of the ruby code to pass the scenario


shoulda-matchers

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/




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@gmail.com"
    password "password"
  end
end

@user = FactoryGirl.build(:user)

No comments:

Post a Comment