0
<ul><li>Test Driven  Development  </li></ul><ul><li>By  </li></ul><ul><li>Uma Mahesh Varma  </li></ul>
Test Drive Development  <ul><li>Introduction </li></ul><ul><li>Importance of T.T.D </li></ul><ul><li>Rails webs applicatio...
What is meant by test drive development ?  <ul><li>The answer for the above question is as fallows, TTD (Test Drive Develo...
Procedure of T.D.D <ul><li>Write a test.   </li></ul><ul><li>Run the test and check whether it fails or not.  </li></ul><u...
Rails webs application directory structure <ul><li>my_app/ </li></ul><ul><li>README Installation and usage information </l...
Test directory  <ul><li>Fixtures  </li></ul><ul><li>Integration </li></ul><ul><li>Functional </li></ul><ul><li>Unit </li><...
Unit Test <ul><li>require File.dirname(__FILE__) + '/../test_helper‘ </li></ul><ul><li>class UserTest < Test::Unit::TestCa...
Unit Test <ul><li>>> ruby test/unit/test_user.rb </li></ul><ul><li>Loaded suite test/unit/user_test </li></ul><ul><li>Star...
Unit Test <ul><li>A Database Just for Tests </li></ul><ul><li>Myapp> mysqladmin -u root create myapp_test </li></ul><ul><l...
Unit Test  <ul><li>myapp > rake db:test:prepare </li></ul><ul><li>Myapp > ruby test/unit/user_test.rb </li></ul><ul><li>Lo...
Unit Test <ul><li>A Real Unit Test </li></ul><ul><li>validates_presence_of :name, :email, :mobilenum </li></ul><ul><li>val...
Unit Test <ul><li>def test_invalid_with_empty_attributes </li></ul><ul><li>user = User.new </li></ul><ul><li>assert ! user...
Unit Test  <ul><li>Myapp > ruby test/unit/user_test.rb </li></ul><ul><li>Loaded suite test/unit/user_test </li></ul><ul><l...
Unit Test <ul><li>def test_image_url </li></ul><ul><li>ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg </li></ul><ul...
Fixtures <ul><li>mahesh: </li></ul><ul><li>id: 1 </li></ul><ul><li>name: mahesh </li></ul><ul><li>email:  [email_address] ...
Unit Test <ul><li>Fixtures :users </li></ul><ul><li>def test_unique_name </li></ul><ul><li>user = User.new(:name => user(:...
Unit test supports  <ul><li>assert(boolean,message) </li></ul><ul><li>assert_equal(expected, actual,message) </li></ul><ul...
Functional Testing of Controllers <ul><li>require ‘user_helper’ </li></ul><ul><li>def UserTest < Test::Unit::Testclass </l...
Functional Testing of Controllers <ul><li>myapp > ruby test/functional/user_test_controller.rb </li></ul><ul><li>Loaded su...
<ul><li>Thank You  </li></ul>
Upcoming SlideShare
Loading in...5
×

Test Drive Development in Ruby On Rails

2,406

Published on

Seminar presentation on Test Drive Development in Ruby on Rails by Uma Mahesh Varma at Nyros Technologies, kakinada, India

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,406
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
25
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Test Drive Development in Ruby On Rails"

  1. 1. <ul><li>Test Driven Development </li></ul><ul><li>By </li></ul><ul><li>Uma Mahesh Varma </li></ul>
  2. 2. Test Drive Development <ul><li>Introduction </li></ul><ul><li>Importance of T.T.D </li></ul><ul><li>Rails webs application directory structure </li></ul><ul><li>Explanation about test folder in rails directory structure </li></ul><ul><li>Unit Tests </li></ul><ul><li>Functional Tests </li></ul>
  3. 3. What is meant by test drive development ?  <ul><li>The answer for the above question is as fallows, TTD (Test Drive Development) is an part of software development techniques based on writing some small test cases before implementing code or new functionalities.  </li></ul><ul><li>Test drive development is nothing but developing a product by writing unit tests that define the code requirement. These tests returns true or false. Running these tests rapidly brings you a correct behavior of the developer to develop an application.  </li></ul>
  4. 4. Procedure of T.D.D <ul><li>Write a test.  </li></ul><ul><li>Run the test and check whether it fails or not. </li></ul><ul><li>If not Write some code. </li></ul><ul><li>Run the automated test. </li></ul><ul><li>Refactor. </li></ul>
  5. 5. Rails webs application directory structure <ul><li>my_app/ </li></ul><ul><li>README Installation and usage information </li></ul><ul><li>Rakefile Build script </li></ul><ul><li>app/ Model, View, and Controller files go here </li></ul><ul><li>components/ Reusable components </li></ul><ul><li>config/ Configuration and database connection parameters </li></ul><ul><li>db/ Schema and migration information </li></ul><ul><li>doc/ Autogenerated documentation </li></ul><ul><li>lib/ Shared code </li></ul><ul><li>log/ Logfiles produced by your application </li></ul><ul><li>public/ Web-accessible directory. Your application runs from here </li></ul><ul><li>script/ Utility scripts </li></ul><ul><li>test/ Unit, functional, and integration tests, fixtures, and mocks </li></ul><ul><li>tmp/ Runtime temporary files </li></ul><ul><li>vendor/ Imported code </li></ul>
  6. 6. Test directory <ul><li>Fixtures </li></ul><ul><li>Integration </li></ul><ul><li>Functional </li></ul><ul><li>Unit </li></ul><ul><li>test_helper.rb </li></ul>
  7. 7. Unit Test <ul><li>require File.dirname(__FILE__) + '/../test_helper‘ </li></ul><ul><li>class UserTest < Test::Unit::TestCase </li></ul><ul><li>fixtures :users </li></ul><ul><li>def test_truth </li></ul><ul><li>assert true </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  8. 8. Unit Test <ul><li>>> ruby test/unit/test_user.rb </li></ul><ul><li>Loaded suite test/unit/user_test </li></ul><ul><li>Started </li></ul><ul><li>EE </li></ul><ul><li>Finished in 0.559942 seconds. </li></ul><ul><li>1) Error: </li></ul><ul><li>test_truth(UserTest): </li></ul><ul><li>MysqlError: Unknown database ‘myapp_test' </li></ul><ul><li>1 tests, 0 assertions, 0 failures, 2 errors </li></ul>
  9. 9. Unit Test <ul><li>A Database Just for Tests </li></ul><ul><li>Myapp> mysqladmin -u root create myapp_test </li></ul><ul><li>Myapp> ruby test/unit/myapp_test.rb </li></ul><ul><li>Loaded suite test/unit/myapp_test </li></ul><ul><li>Started </li></ul><ul><li>E </li></ul><ul><li>Finished in 0.06429 seconds. </li></ul><ul><li>1) Error: </li></ul><ul><li>test_truth(ProductTest): </li></ul><ul><li>ActiveRecord::StatementInvalid: MysqlError: </li></ul><ul><li>Table ‘myapp_test. users' doesn't exist: DELETE FROM users </li></ul><ul><li>1 tests, 0 assertions, 0 failures, 1 errors </li></ul>
  10. 10. Unit Test <ul><li>myapp > rake db:test:prepare </li></ul><ul><li>Myapp > ruby test/unit/user_test.rb </li></ul><ul><li>Loaded suite test/unit/user_test </li></ul><ul><li>Started </li></ul><ul><li>. </li></ul><ul><li>Finished in 0.085795 seconds. </li></ul><ul><li>1 tests, 1 assertions, 0 failures, 0 errors </li></ul>
  11. 11. Unit Test <ul><li>A Real Unit Test </li></ul><ul><li>validates_presence_of :name, :email, :mobilenum </li></ul><ul><li>validates_numericality_of : mobilenum </li></ul><ul><li>validates_uniqueness_of :name </li></ul><ul><li>validates_format_of :image_url, </li></ul><ul><li>:with => %r{.(gif|jpg|png)$}i, </li></ul><ul><li>:message => &quot;must be a URL for a GIF, JPG, or PNG image&quot; </li></ul>
  12. 12. Unit Test <ul><li>def test_invalid_with_empty_attributes </li></ul><ul><li>user = User.new </li></ul><ul><li>assert ! user .valid? </li></ul><ul><li>assert user .errors.invalid?(:name) </li></ul><ul><li>assert user .errors.invalid?(:email) </li></ul><ul><li>assert user .errors.invalid?(:mobileno) </li></ul><ul><li>assert user.errors.invalid?(:image_url) </li></ul><ul><li>end </li></ul>
  13. 13. Unit Test <ul><li>Myapp > ruby test/unit/user_test.rb </li></ul><ul><li>Loaded suite test/unit/user_test </li></ul><ul><li>Started </li></ul><ul><li>.. </li></ul><ul><li>Finished in 0.092314 seconds. </li></ul><ul><li>2 tests, 6 assertions, 0 failures, 0 errors </li></ul>
  14. 14. Unit Test <ul><li>def test_image_url </li></ul><ul><li>ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg </li></ul><ul><li>http://a.b.c/x/y/z/fred.gif } </li></ul><ul><li>bad = %w{ fred.doc fred.gif/more fred.gif.more } </li></ul><ul><ul><li>ok.each do |name| </li></ul></ul><ul><ul><ul><li>user = User.new(:name => “mahesh&quot; , </li></ul></ul></ul><ul><ul><ul><li>:email => “umamahesh_nyros@yahoo.com&quot; , </li></ul></ul></ul><ul><ul><ul><li>:mobilenum => 9866439593, </li></ul></ul></ul><ul><ul><ul><li>:image_url => name) </li></ul></ul></ul><ul><ul><ul><li>assert user.valid?, usererrors.full_messages </li></ul></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>bad.each do |name| </li></ul></ul><ul><ul><ul><li>user = User.new(:name => “mahesh&quot; , </li></ul></ul></ul><ul><ul><ul><li>:email => “umamahesh_nyros@yahoo.com&quot; , </li></ul></ul></ul><ul><ul><ul><li>:mobilenum => 9866439593, </li></ul></ul></ul><ul><ul><ul><li>:image_url => name) </li></ul></ul></ul><ul><ul><ul><li>assert user.valid?, usererrors.full_messages </li></ul></ul></ul><ul><ul><li>end </li></ul></ul>
  15. 15. Fixtures <ul><li>mahesh: </li></ul><ul><li>id: 1 </li></ul><ul><li>name: mahesh </li></ul><ul><li>email: [email_address] </li></ul><ul><li>mobilenum: 9866439593 </li></ul><ul><li>image_url: maheshprofile.jpg </li></ul>
  16. 16. Unit Test <ul><li>Fixtures :users </li></ul><ul><li>def test_unique_name </li></ul><ul><li>user = User.new(:name => user(:mahesh).name, </li></ul><ul><li>:email => “umamahesh_nyros@yahoo.com&quot; , </li></ul><ul><li>:mobilenum => 9866439593, </li></ul><ul><li>:image_url => &quot;fred.gif&quot; ) </li></ul><ul><li>assert !user.save </li></ul><ul><li>assert_equal &quot;has already been taken&quot; , user.errors.on(:name) </li></ul><ul><li>end </li></ul>
  17. 17. Unit test supports <ul><li>assert(boolean,message) </li></ul><ul><li>assert_equal(expected, actual,message) </li></ul><ul><li>assert_not_equal(expected, actual,message) </li></ul><ul><li>assert_nil(object,message) </li></ul><ul><li>assert_not_nil(object,message) </li></ul><ul><li>assert_match(pattern, string,message) </li></ul><ul><li>assert_no_match(pattern, string,message) </li></ul>
  18. 18. Functional Testing of Controllers <ul><li>require ‘user_helper’ </li></ul><ul><li>def UserTest < Test::Unit::Testclass </li></ul><ul><li>def test_index </li></ul><ul><li>assert true </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  19. 19. Functional Testing of Controllers <ul><li>myapp > ruby test/functional/user_test_controller.rb </li></ul><ul><li>Loaded suite test/functional/ user_test_controller.rb </li></ul><ul><li>Started </li></ul><ul><li>. </li></ul><ul><li>Finished in 0.0604571 seconds. </li></ul><ul><li>1 tests, 1 assertions, 0 failures, 0 errors </li></ul>
  20. 20. <ul><li>Thank You </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×