Tuesday, April 10, 2012

Test Driven Development (TDD) in ROR(Ruby On Rails)

Test Driven Development is a development practice which involves writing test cases before writing the code. Start by writing a very small test for code that does not yet exist. Run the test and, naturally, it fails. Now we have to write the code to pass the test.ie, writing code only for the requirement. Large numbers of tests help to limit the number of defects in the code. The early and frequent nature of the testing helps to catch defects early in the development cycle, preventing them from becoming endemic and expensive problems.

        Ruby on rails supports test frameworks for Test Driven Development; A Test Framework is a tool or library that provides a backdrop for writing tests. This is an excellent way to build up a test suite that exercises every part of the application.

There are several testing frameworks in use for Ruby today:
Test::Unit is included with Ruby 1.8, and follows the "xUnit" conventions
  • Minitest is included with Ruby 1.9, and allows both xUnit and RSpec style tests
  • RSpec, which  has more concise syntax and can be used in the same project, but creates a separate suite of tests, called "specs"
  • In Cucumber, tests are written not in Ruby but in a language designed for tests
  • Rspec-2, which is used with Rails -3.
Work Flow
  • First write a test: This test describes the behavior of a small element of your system. Tests in TDD are called programmer tests. When writing the tests it should be kept in mind that the tests should concentrate on testing the true behaviors, i.e. if a system has to handle multiple inputs, the tests should reflect multiple inputs.
  • Run the test:  The test definitely fails because you have not yet built the code for that part of your system. This important step tests your test case, verifying that your test case fails. The automatic tests should be run after each change of the application code in order to assure that the changes have not introduced errors to the previous version of the code
  • Write Code: Then only real coding comes, write enough code to make the test pass. In TDD, the code writing is actually a process for making the test work, i.e. writing the code that passes the test.
  • Run the test: Again run the test and verify that they pass.
·         Refactor the code - Refactoring is a process of improving the internal structure by editing the existing working code, without changing its external behavior. The idea of refactoring is to carry out the modifications as a series of small steps without introducing new defects into to the system
  • Run all tests:  To verify that the refactoring did not change the external behavior.
Hope Now you Had some Idea abt TDD...

1 comment:

  1. Nice post. We work on emerging new technologies in order to create rich web applications using ROR development platform. We follow agile development methodology to deliver solutions that meets our customer’s specific business requirements.

    ROR Development

    ReplyDelete