Presented by Nazia Hossain Nascenia IT www.nascenia.com
Testing in Software Engineering Testing is to determine if the requirements of the application are met Classification: White box testing Black box testing  www.nascenia.com
Classification White Box Testing: - Tests specific paths through the code - Tests decision points Black Box Testing: - Treats the program like black-box - Tests specific input & output www.nascenia.com
TDD (Test-Driven Development)  Repetition of very short development cycle Test-first programming concept Write some code Run the automated tests and see them succeed Refactor code Repeat  www.nascenia.com
BDD (Behavior-Driven Development) Encourages collaboration between developers and non-technicals  Clear understanding of desired software behaviour  Combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning  Focuses on the reason of why the code should be created www.nascenia.com
RSpec BDD framework for the Ruby Programming Language Has own mocking framework  Similar to a natural language specification www.nascenia.com
Installation RSpec In Gem-file: group :test, :development do gem 'rspec-rails' end  Install >gem install rspec-rails Install >bundle install www.nascenia.com
Run RSpec Run >db:migrate Run >rake rspec www.nascenia.com
Run Rspec(cont.) After installation, there is a RSpec directory Contains Spec directory of: Controllers  Helpers Models Requests Routings Views etc www.nascenia.com
Example: Suppose, One has some dreams, He wants to list them and wish to complete within 10years. So, he has a Controller like “Dream”,  - Has a Model of the same name. - Has a view to see the dreams. We are going to test the controllers, models and views mainly if he makes his dream correctly. www.nascenia.com
RSpec with Controllers Goal is to create very simple, easy to read One assertion per test Stub everything (actually we use “Factory girl”) Emphasis on Controllers and Models to test www.nascenia.com
RSpec with Controllers(cont.) First, add “spec_helper” in the controller spec file. Like, in “dreams_controller_spec” The file contains: ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails‘ www.nascenia.com
RSpec with Controllers(cont.) First, do the things once, that can be repeated many times. Like: at first, create a Dream instance  Then add setup before(:all) do @dream = Dream.create! end At the end Teardown after(:all) do @dream.delete end www.nascenia.com
RSpec with Controllers(cont.) Suppose, he has a show method, that can show a particular dream def show @dream  = Dream.find(params[:id]) respond_with(@dream) End Simply, finds a “Dream” using a particular “id”, show this with “@dream” www.nascenia.com
RSpec with Controllers(cont.) Now, we’ve to test the show, like, describe "GET show" do it "assigns the requested dream as @dream" do get :show, :id => @dream1.id.to_s assigns(:dream).should eq(@dream1) end www.nascenia.com
RSpec with Controllers(cont.) First, “show” is a “GET” method, So, we use to describe “GET show” Then assigns the “dream” from our test to “@dream” of the controller to test if the match Now, because the route like,  dreams/:id/show - So, we pass  :id => @dream.id www.nascenia.com
RSpec with Controllers(cont.) Then assigns the “@dream” from test controller to “:dream” in controller (they should be equal!) assigns(:dream)  @dream=Dream.find(params[:id]) should eq(@dream1)  @dream www.nascenia.com
Response with Rspec Some, responses are given below:  response.should be_success response.should render_template("complete_form") response.should redirect_to(dreams_path) www.nascenia.com
RSpec with Models Factory Girl: Gem, used to create objects Similar to use fixtures Factory.define :dream, :class=>Dream do |f| f.name  ‘dreamname1’ end www.nascenia.com
RSpec with Models(cont.) Again, he has a “Dream” Model So, we have to create a stub “Dream” model to test this This can be done using  factory_girl Suppose, he has a validation - “Every Dream should have a name” www.nascenia.com
RSpec with Models(cont.) Describe Dream do before(:all) do @dream=Factory(:dream) end It “should have name” do @dream.name=‘’ @dream.should_no be_valid end end www.nascenia.com
RSpec with Views Suppose, he wants to view all the dreams and has a “index.html” file So, again at first, we have to make a stub array of “Dream” model  And if it renders the “index.html” then, the test is succeed www.nascenia.com
RSpec with Views(cont.) describe "dreams/index.html.erb" do before(:each) do assign(:dreams, [ stub_model(Dream), stub_model(Dream) ]) end it "renders a list of dreams" do render end end www.nascenia.com
Rcov Code coverage tool for Ruby Viewing overall test unit coverage of target code Write  gem ‘rcov’  in Gemfile Install> gem install rcov Run >rake spec:rcov From “project/coverage/index.html”, we can see the total coverage in percentage www.nascenia.com
Rcov Report www.nascenia.com
Thank You www.nascenia.com

TDD, BDD, RSpec

  • 1.
    Presented by NaziaHossain Nascenia IT www.nascenia.com
  • 2.
    Testing in SoftwareEngineering Testing is to determine if the requirements of the application are met Classification: White box testing Black box testing www.nascenia.com
  • 3.
    Classification White BoxTesting: - Tests specific paths through the code - Tests decision points Black Box Testing: - Treats the program like black-box - Tests specific input & output www.nascenia.com
  • 4.
    TDD (Test-Driven Development) Repetition of very short development cycle Test-first programming concept Write some code Run the automated tests and see them succeed Refactor code Repeat www.nascenia.com
  • 5.
    BDD (Behavior-Driven Development)Encourages collaboration between developers and non-technicals Clear understanding of desired software behaviour Combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning Focuses on the reason of why the code should be created www.nascenia.com
  • 6.
    RSpec BDD frameworkfor the Ruby Programming Language Has own mocking framework Similar to a natural language specification www.nascenia.com
  • 7.
    Installation RSpec InGem-file: group :test, :development do gem 'rspec-rails' end Install >gem install rspec-rails Install >bundle install www.nascenia.com
  • 8.
    Run RSpec Run>db:migrate Run >rake rspec www.nascenia.com
  • 9.
    Run Rspec(cont.) Afterinstallation, there is a RSpec directory Contains Spec directory of: Controllers Helpers Models Requests Routings Views etc www.nascenia.com
  • 10.
    Example: Suppose, Onehas some dreams, He wants to list them and wish to complete within 10years. So, he has a Controller like “Dream”, - Has a Model of the same name. - Has a view to see the dreams. We are going to test the controllers, models and views mainly if he makes his dream correctly. www.nascenia.com
  • 11.
    RSpec with ControllersGoal is to create very simple, easy to read One assertion per test Stub everything (actually we use “Factory girl”) Emphasis on Controllers and Models to test www.nascenia.com
  • 12.
    RSpec with Controllers(cont.)First, add “spec_helper” in the controller spec file. Like, in “dreams_controller_spec” The file contains: ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails‘ www.nascenia.com
  • 13.
    RSpec with Controllers(cont.)First, do the things once, that can be repeated many times. Like: at first, create a Dream instance Then add setup before(:all) do @dream = Dream.create! end At the end Teardown after(:all) do @dream.delete end www.nascenia.com
  • 14.
    RSpec with Controllers(cont.)Suppose, he has a show method, that can show a particular dream def show @dream = Dream.find(params[:id]) respond_with(@dream) End Simply, finds a “Dream” using a particular “id”, show this with “@dream” www.nascenia.com
  • 15.
    RSpec with Controllers(cont.)Now, we’ve to test the show, like, describe "GET show" do it "assigns the requested dream as @dream" do get :show, :id => @dream1.id.to_s assigns(:dream).should eq(@dream1) end www.nascenia.com
  • 16.
    RSpec with Controllers(cont.)First, “show” is a “GET” method, So, we use to describe “GET show” Then assigns the “dream” from our test to “@dream” of the controller to test if the match Now, because the route like, dreams/:id/show - So, we pass :id => @dream.id www.nascenia.com
  • 17.
    RSpec with Controllers(cont.)Then assigns the “@dream” from test controller to “:dream” in controller (they should be equal!) assigns(:dream) @dream=Dream.find(params[:id]) should eq(@dream1) @dream www.nascenia.com
  • 18.
    Response with RspecSome, responses are given below: response.should be_success response.should render_template("complete_form") response.should redirect_to(dreams_path) www.nascenia.com
  • 19.
    RSpec with ModelsFactory Girl: Gem, used to create objects Similar to use fixtures Factory.define :dream, :class=>Dream do |f| f.name ‘dreamname1’ end www.nascenia.com
  • 20.
    RSpec with Models(cont.)Again, he has a “Dream” Model So, we have to create a stub “Dream” model to test this This can be done using factory_girl Suppose, he has a validation - “Every Dream should have a name” www.nascenia.com
  • 21.
    RSpec with Models(cont.)Describe Dream do before(:all) do @dream=Factory(:dream) end It “should have name” do @dream.name=‘’ @dream.should_no be_valid end end www.nascenia.com
  • 22.
    RSpec with ViewsSuppose, he wants to view all the dreams and has a “index.html” file So, again at first, we have to make a stub array of “Dream” model And if it renders the “index.html” then, the test is succeed www.nascenia.com
  • 23.
    RSpec with Views(cont.)describe "dreams/index.html.erb" do before(:each) do assign(:dreams, [ stub_model(Dream), stub_model(Dream) ]) end it "renders a list of dreams" do render end end www.nascenia.com
  • 24.
    Rcov Code coveragetool for Ruby Viewing overall test unit coverage of target code Write gem ‘rcov’ in Gemfile Install> gem install rcov Run >rake spec:rcov From “project/coverage/index.html”, we can see the total coverage in percentage www.nascenia.com
  • 25.
  • 26.

Editor's Notes

  • #3 www.nascenia.com