29
Testing Technique By:- Arvind Vyas

TDD & BDD

Embed Size (px)

DESCRIPTION

This has some details about what TDD and what is BDD, also it will help to the beginner to understand what exactly TDD and BDD covers and what it is

Citation preview

Page 1: TDD & BDD

Testing Technique By:- Arvind Vyas

Page 2: TDD & BDD

What is TDD and BDD ?

Page 3: TDD & BDD

Test Driven Development

Page 4: TDD & BDD

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. The developer can then refactor his code, add comments, clean it up, as he wishes because the developer knows that

if the new code breaks something, then the tests will alert him by failing.

Test Driven Development

Page 5: TDD & BDD

Flow Chart

Page 6: TDD & BDD

Behaviour driven development

Page 7: TDD & 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.

BDD

Page 8: TDD & BDD

#Testing for our User classdescribe 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

Page 9: TDD & BDD

Example for TDD and BDD approach

Page 10: TDD & BDD

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));

});

});});

Page 11: TDD & BDD

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);

});

});

after(function () {

// Anything after the tests have finished

});

Page 12: TDD & BDD

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.

Page 13: TDD & BDD
Page 14: TDD & BDD

WHY TESTING ????????

Page 15: TDD & BDD

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

Page 16: TDD & BDD

● Unit Tests● Integration Tests● Acceptance Tests

Levels of Testing

Page 17: TDD & BDD
Page 18: TDD & BDD
Page 19: TDD & BDD

Ready ????

Page 20: TDD & BDD

$ ls -F test

Page 21: TDD & BDD

controllers/ helpers/ mailers/ test_helper.rb fixtures/ integration/

models/

Page 22: TDD & BDD

$ rails new TestingTime$rails generate scaffold profile title:string body:text$ rake test test/models/profile_test.rb

Unit Test???

Page 23: TDD & BDD

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 Post < ActiveRecord::Base

validates :title, :body, presence:

true

end

class Profile < ActiveRecord::Base

validates :title, :body, presence: true

end

$ rake test test/models/post_test.rb

Page 24: TDD & BDD

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.

Controllers ??

Page 25: TDD & BDD

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

Page 26: TDD & BDD

#run all the test case which are inside test$ rake test testi

Page 27: TDD & BDD

->Web request successful-> User redirect to the right place-> User successfully authenticated -> Correct object stored in the response template- > Appropriate message displayed

What to include in your functional test

Page 28: TDD & BDD

What is the use of integration folder in testing what we put on that ?? ????

A Last question for all

Page 29: TDD & BDD