Test Driven Development in Java
Test driven development a software development methodology which ensure at an early stage that the code written by the developer is what it is supposed to do, which eventually improves the design, produces testable code and reduces the amount of re-engineering that would have been necessary because of the wrong assumptions. Test Driven Development (TDD) is widely used in extreme Programming. This document gives an overview on TDD.
Drawback with the traditional testing
- Testing Poorly Written Code: Defects with the software system makes it unusable and the approach followed for getting rid of defect is by testing the code and trying to break it. But testing once the code is frozen is a lengthy, slow and a costly process to further develop it.
- Maintenance: Poorly written code apart from having a bad design is also difficult to change, since it’s difficult to understand the code. Even if the modifications are incorporated there can be some sideeffects on other parts of the functionality.
- Failure to meet the requirement: Failing to deliver as per the requirement of the customer on time would not only lead to a delayed delivery but also extends the deadline , the requirement of the customer also changes during this period and the process keeps on going
The Solution -Test Driven Development(TDD)
What is TDD?
“Before we write the code, think about what your code will do.Write a test that will use the methods that hasn’t been written yet.”
TDD completely twirl traditional development around Kent Beck, had popularized TDD in eXtreme Programming he defines 2 simple rules one is write new business code only when your automated test has failed. second ,as you keep changing remove any duplication that you find. Beck also explained how these two simple rules generate complex individual and group behavior
On a lower level we test-drive code using the technique we call TDD,on a higher level-that of features and funcunality of the system -we test drive the system using a similar technique we call a acceptance TDD.
The advantage of TDD is
High Quality with TDD:as this way of programming that encourages good design and is a disciplined process that helps us avoid programming errors.This is achieved by making us write small,automated tests,which eventually build up a small alarm system for protecting our code from regression.
The short development cycle that TDD promotes is a well geared toward writing high-quality code from the start. Due to the short cycle we deviate from the traditional development methodology.
i.e. Design->Implement->Test the implementation
instead we follow
i.e write the test first and then write the code to see that the test passes and finally refractor to remove any duplication and taking the code toward the best design we can imagine.
Improves the Quality:Quality of a s/w can be measured in terms of the degree to which the s/w fulfils the users requirement as well as in terms of the internal quality of the software that translates to external qualities like cost of development ,maintenance etc.by following TDD ,its assured that there is no code in the system that is developed that is unwanted.
Less time in Fixing code: Finding and fixing defects at an early stage is what we achieve practicing TDD which invariably reduces the development time.
TDD helps us to write code having high technical quality .since the correctness of the code we develop with TDD is for isolated block of code rather than for the feature or functionality of the software .Acceptance TDD bridges the gap between the coder and the customer .Acceptance TDD tests the behaviour of the system rather than an single Object.
As already discussed when following the TDD approach we write the test first and then code and finally, design also
called the refractor phase as shown in figure-1
Test :When we are writing the test ,we are actually designing the API envisioning what the functionality of our code could be.
Code: When we talk about code we try to write the code to ensure that our test pass, when the test passes it doesn’t mean that the code written by us is optimal,but we keep improving the design in the last step Refractor.
Refractor: In the final step we look into how we can make our code more optimal and invariantly the design as well.
Tools of TDD
Just like any other type of development we require tools and technique for TTD too and there are 3 different categories
- Unit Testing Framework
- Continuous integration and builds
- Code coverage
Unit testing framework for TDD :There are frameworks available for unit testing our code.One of the most popular framework for Java is JUnit built around XUnit which provides us with base classes which is extended for writing our tests,different test runners that helps us execute and collect the test results.
When it come to framework for acceptance TDD we don’t have a base framework as such since the idea of test-driving on feature level is relatively new.
As we keep constantly changing our code as we try to improve it, there is constant requirement to integrate their changes more often thus we need to have to adopt a process to synchronize our changes to the source storehouse. Apart from integration we also need to check that the integrated source code works too.A common practice is to run a relevant subset of all test before checking in their changes and to delegate the running of the full test suite to a dedicated build server also known as Continuous build server which keeps polling for any changes in the source repository and only tests those relevant portions rather than testing the entire code which would take lot of time.There are tools available to write the build servers.
There are tools available to measure code coverage or test coverage which detect any anomalies in our code in terms of wrong usage of constructs etc.Code coverage measures how thoroughly the test cases look through our production code.
Latest posts by Krishna Srinivasan (see all)
- 5 New Features in HTML5 - November 29, 2013
- How To Set Tomcat JVM Heap Size in Eclipse? - November 28, 2013
- How To Resolve “Resource Is Out Of Sync With The Filesystem” Error in Eclipse? - November 28, 2013
Pages: 1 2