TDD & BDD

  • View
    173

  • Download
    18

  • Category

    Software

Preview:

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

Testing Technique By:- Arvind Vyas

What is TDD and BDD ?

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. 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

Flow Chart

Behaviour driven development

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

#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

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

});

});

after(function () {

// Anything after the tests have finished

});

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

● Unit Tests● Integration Tests● Acceptance Tests

Levels of Testing

Ready ????

$ ls -F test

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

models/

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

Unit Test???

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

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 ??

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 testi

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

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

A Last question for all

Recommended