Skip to content

Test before! Rest after!!

April 14, 2013

There are many different articles, white papers and journals about testing and why we need Test Driven Development (TDD). But i wrote this post for who does not know about TDD or how to start this model of development. Here you can find out more about Kent Beck, the man who rediscovered this technique : Kent Beck
TDD is very related to the Test First concept in Extreme Programming (XP), started from 1999. In this model of development we have short iteration of development, every iteration starts by a failed test. Then developer will write code just until the test and every past tests passed. At first maybe it’s strange at first,but there are many reasons that justified this approach is effective and applicable. As an formal steps in TDD there are these step:

  • Test: In this step all your previous test has been passed, then you write a new test and run all the tests, your last test must fail, if not there is a problem in your test.
  • Code: In this step you write some code, the goal is to pass the last failed test. passing last test will make a new feature in your application. Then you run all the tests and if all of them was passed this step completed.
  • Refactor: In this step you need to refactor your code and your tests for eliminating duplicate, boilerplate, unclean codes, smell codes. There are many best practice about refactoring and its patterns that you can use them.
  • Repeat from Step 1 with your new feature.

Some augmented steps before last step can add, these optional steps will increase the quality of your code. These Steps are:

  • Document: write document for your code API. these documents will help you and other clients to find out how must use your API.
  • Log: write logs in appropriate points of your codes. then you can track the problems and events in your code more easy than before.

Maybe you have seen Red/Green development some where. it’s another name for Test First development, because at first your write a failed test ( most of test framework show them as red). Then you write some code for passing them ( most of test framework show them as green).

Writing tests before code have many advantages, you can see some of them below:

  • You don’t to write testable code. Hah? yesss. you have written test before code so your code will be testable by default.
  • Your Code Coverage will be more than 95%. If you write code just for passing your failed test you will achive this big advantage.
  • You have a history of your thinking forever. when you are writing a test you have some opinion about application. you have all of them. and you know which part of your code is for which test, So you have created a document about your code in development cycle.
  • Your API will be very handy for clients, because you have written API when you were writing the test, without any technical issues

In this post i wanted to have an overview on TDD and its advantages. In the next post we will start writing test and introducing its concepts.

Let me know about your opinions and feedback.


From → JAVA, JAVASE, Test

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: