Testing Technique 
By:- Arvind Vyas
What is TDD and BDD ?
Test Driven Development
Test Driven Development 
When I first heard about TDD, the idea seemed to be pretty simple. Just by doing a little word swizzling, obviously TDD 
is when you have tests that drive your software development. 
If we were to unpack the definition of TDD a bit more, we'd see that it is usually broken up into five different stages: 
1. First the developer writes some tests. 
2. The developer then runs those tests and (obviously) they fail because none of those features are actually 
implemented. 
3. Next the developer actually implements those tests in code. 
4. If the developer writes his code well, then the in next stage he will see his tests pass.
Flow Chart
Behaviour driven development
BDD 
BDD is meant to eliminate issues that TDD might cause. 
In contrast to TDD, BDD is when we write behavior & 
specification that then drives our software development. 
Behavior & specification might seem awfully similar to 
tests but the difference is very subtle and important.
Example 
#Testing for our User class 
describe User do 
context 'with admin privileges' do 
before :each do 
@admin = Admin.get(1) 
end 
it 'should exist' do 
expect(@admin).not_to be_nil 
end 
it 'should have a name' do 
expect(@admin.name).not_to be_falsy 
end 
end 
#...
Example for TDD and BDD 
approach
var assert = require('assert'), 
factorial = require('../index');suite('Test', function (){ 
setup(function (){ 
// Create any objects that we might need 
}); 
suite('#factorial()', function (){ 
test('equals 1 for sets of zero length', function (){ 
assert.equal(1, factorial(0)); 
}); 
test('equals 1 for sets of length one', function (){ 
assert.equal(1, factorial(1)); 
}); 
test('equals 2 for sets of length two', function (){ 
assert.equal(2, factorial(2)); 
}); 
test('equals 6 for sets of length three', function (){ 
assert.equal(6, factorial(3)); 
}); 
});});
var assert = require('assert'), 
factorial = require('../index');describe('Test', function (){ 
before(function(){ 
// Stuff to do before the tests, like imports, what not 
}); 
describe('#factorial()', function (){ 
it('should return 1 when given 0', function (){ 
factorial(0).should.equal(1); 
}); 
it('should return 1 when given 1', function (){ 
factorial(1).should.equal(1); 
}); 
it('should return 2 when given 2', function (){ 
factorial(2).should.equal(2); 
}); 
it('should return 6 when given 3', function (){ 
factorial(3).should.equal(6); 
}); 
});
If you have seen the previous example then 
you can find it out that the first example 
for the TDD because inside the developer 
only checking the value of the method 
But if you have observed the second code 
then you will find test are more focus on 
the behaviour.
WHY TESTING ????????
1) By simply running your rails test you can ensure your 
code adheres to the desired functionality even after some 
major code refactoring. 
2) Your application response without having to test it 
through your browser 
3) You can easily upgrade your rails application
Levels of Testing 
● Unit Tests 
● Integration Tests 
● Acceptance Tests
Ready ????
$ ls -F test
controllers/ helpers/ mailers/ 
test_helper.rb fixtures/ integration/ 
models/
Unit Test??? 
$ rails new TestingTime 
$rails generate scaffold profile 
title:string body:text 
$ rake test test/models/profile_test.rb
require 'test_helper' 
class ProfileTest < ActiveSupport::TestCase 
test "the truth" do 
assert true 
end 
test "should not have Profile with title" do 
profile = Profile.new 
assert_not profile.save # save the title 
without title 
end 
test "should not have profile without body" do 
profile = Profile.new 
profile.title = 'Arvind' 
assert_not profile.save # save the body without 
title 
end 
end 
class Profile < ActiveRecord::Base 
class Post < ActiveRecord::Base 
validates :title, :body, presence: true 
validates :title, :body, presence: true 
end 
end 
$ rake test test/models/post_test.rb
Controllers ?? 
Testing the various actions of a single 
controller is called writing functional tests 
for that controller. 
Controllers handle the incoming web 
requests to your application and eventually 
respond with a rendered view.
require 'test_helper' 
class ProfilerofileControllerTest < ActionController::TestCase 
setup do 
@rofileprofile = rofilesprofile(:one) 
end 
test "should get index" do 
get :index 
assert_response :success 
assert_not_nil assigns(:profile) 
end 
test "should get new" do 
get :new 
assert_response :success 
end 
test "should create rofileprofile" do 
assert_difference('Profilerofile.count') do 
post :create, profile: {title: @profrofile.title, body: @profile.body} 
end 
assert_redirected_to profile_path(assigns(:profile)) 
assert_equal 'Profile was successfully created.', flash[:notice] 
end
#run all the test case which are inside test 
$ rake test test 
i
What to include in your 
functional test 
->Web request successful 
-> User redirect to the right place 
-> User successfully authenticated 
-> Correct object stored in the response 
template 
- > Appropriate message displayed
A Last question for all 
What is the use of integration folder in 
testing what we put on that ?? ????
TDD & BDD

TDD & BDD

  • 1.
  • 2.
    What is TDDand BDD ?
  • 3.
  • 4.
    Test Driven Development When I first heard about TDD, the idea seemed to be pretty simple. Just by doing a little word swizzling, obviously TDD is when you have tests that drive your software development. If we were to unpack the definition of TDD a bit more, we'd see that it is usually broken up into five different stages: 1. First the developer writes some tests. 2. The developer then runs those tests and (obviously) they fail because none of those features are actually implemented. 3. Next the developer actually implements those tests in code. 4. If the developer writes his code well, then the in next stage he will see his tests pass.
  • 5.
  • 6.
  • 7.
    BDD BDD ismeant to eliminate issues that TDD might cause. In contrast to TDD, BDD is when we write behavior & specification that then drives our software development. Behavior & specification might seem awfully similar to tests but the difference is very subtle and important.
  • 8.
    Example #Testing forour User class describe User do context 'with admin privileges' do before :each do @admin = Admin.get(1) end it 'should exist' do expect(@admin).not_to be_nil end it 'should have a name' do expect(@admin.name).not_to be_falsy end end #...
  • 9.
    Example for TDDand BDD approach
  • 10.
    var assert =require('assert'), factorial = require('../index');suite('Test', function (){ setup(function (){ // Create any objects that we might need }); suite('#factorial()', function (){ test('equals 1 for sets of zero length', function (){ assert.equal(1, factorial(0)); }); test('equals 1 for sets of length one', function (){ assert.equal(1, factorial(1)); }); test('equals 2 for sets of length two', function (){ assert.equal(2, factorial(2)); }); test('equals 6 for sets of length three', function (){ assert.equal(6, factorial(3)); }); });});
  • 11.
    var assert =require('assert'), factorial = require('../index');describe('Test', function (){ before(function(){ // Stuff to do before the tests, like imports, what not }); describe('#factorial()', function (){ it('should return 1 when given 0', function (){ factorial(0).should.equal(1); }); it('should return 1 when given 1', function (){ factorial(1).should.equal(1); }); it('should return 2 when given 2', function (){ factorial(2).should.equal(2); }); it('should return 6 when given 3', function (){ factorial(3).should.equal(6); }); });
  • 12.
    If you haveseen the previous example then you can find it out that the first example for the TDD because inside the developer only checking the value of the method But if you have observed the second code then you will find test are more focus on the behaviour.
  • 14.
  • 15.
    1) By simplyrunning your rails test you can ensure your code adheres to the desired functionality even after some major code refactoring. 2) Your application response without having to test it through your browser 3) You can easily upgrade your rails application
  • 16.
    Levels of Testing ● Unit Tests ● Integration Tests ● Acceptance Tests
  • 19.
  • 20.
    $ ls -Ftest
  • 21.
    controllers/ helpers/ mailers/ test_helper.rb fixtures/ integration/ models/
  • 22.
    Unit Test??? $rails new TestingTime $rails generate scaffold profile title:string body:text $ rake test test/models/profile_test.rb
  • 23.
    require 'test_helper' classProfileTest < ActiveSupport::TestCase test "the truth" do assert true end test "should not have Profile with title" do profile = Profile.new assert_not profile.save # save the title without title end test "should not have profile without body" do profile = Profile.new profile.title = 'Arvind' assert_not profile.save # save the body without title end end class Profile < ActiveRecord::Base class Post < ActiveRecord::Base validates :title, :body, presence: true validates :title, :body, presence: true end end $ rake test test/models/post_test.rb
  • 24.
    Controllers ?? Testingthe various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.
  • 25.
    require 'test_helper' classProfilerofileControllerTest < ActionController::TestCase setup do @rofileprofile = rofilesprofile(:one) end test "should get index" do get :index assert_response :success assert_not_nil assigns(:profile) end test "should get new" do get :new assert_response :success end test "should create rofileprofile" do assert_difference('Profilerofile.count') do post :create, profile: {title: @profrofile.title, body: @profile.body} end assert_redirected_to profile_path(assigns(:profile)) assert_equal 'Profile was successfully created.', flash[:notice] end
  • 26.
    #run all thetest case which are inside test $ rake test test i
  • 27.
    What to includein your functional test ->Web request successful -> User redirect to the right place -> User successfully authenticated -> Correct object stored in the response template - > Appropriate message displayed
  • 28.
    A Last questionfor all What is the use of integration folder in testing what we put on that ?? ????