CSC/ECE 517 Fall 2012/ch2a 2w31 up
SaaS - 5.3 - The TDD cycle: red-green-refactor
Introduction
Test Driven Development is an evolutionary approach to software development that requires a developer to write test code before the actual code and then write the minimum code to pass that test. This process is done iteratively to ensure that all units in the application are tested for optimum functionality, both individually and in synergy with others. This produces applications of high quality in less time.
Advantages and Disadvantages
Advantages
ensures the code is tested and enables you to retest your code quickly and easily, since it’s automated.
Red – Green – Refactor
Add a Test
- Think about one thing the code should do : The developer identifies a new functionality from the use cases and user stories, which contain detailed requirements and constraints.
- Capture that thought in a test, which fails : An automated test case (new or a variant of an existing test) is then written, corresponding to the new feature, taking into consideration all possible inputs, error conditions and outputs. Run all the automated tests. The new test inevitably fails because it is written prior to the implementation of the feature. This validates that the feature is rightly tested and would pass if the feature is implemented correctly, which drives a developer to the next step.
Implement the feature
- Write the simplest possible code that lets the test pass : Minimal code is written to make the test pass. The entire functionality need not be implemented in this step. It is not uncommon to see empty methods or methods that simply return a constant value. The code can be improved in the next iterations. Future tests will be written to further define what these methods should do. The only intention of the developer is to write "just enough" code to ensure it meets all the tested requirements and doesn't cause any other tests to fail. <ref>What is Test Driven Development?</ref> Run the tests again. Ideally, all the tests should pass, making the developer confident about the features implemented so far.
Refactor
- DRY out commonality with other tests : Remove duplication of code wherever possible. Organizational changes can be made as well to make the code appear cleaner so it’s easier to maintain. TDD encourages frequent refactoring. Automated tests can be run to ensure the code refactoring did not break any existing functionality
Iterate
- Continue with the next thing (new or improvement of a feature), the code should do.
- Aim to have working code always.
Examples
TMDb : The Movie Database rails application
New Feature : Search TMDb for movies
To add a new feature to this Rails application, we first add a route, which maps a URL to the controller method <ref>Routing in Rails</ref>
Adding the route to config/routes.rb
- Route that posts 'Search TMDb' form
post '/movies/search_tmdb'
– Convention over configuration will map this to
MoviesController#search_tmdb
• Create an empty view:!
touch app/views/movies/search_tmdb.html.haml
• Replace fake “hardwired” method in movies_controller.rb with empty method:!
def search_tmdb end
References
<references/>