Test Driven Development - TFC

In test first coding, as we write the test code before writing the class, we are motivated to think about how our class will be used. Without it we focus more on implementation. This leads to a design that is simpler and pragmatic. However, once we get into developing the code, the unit tests can pretty much drive our design.

There are several benefits that one could see from an approach like this:

  1. Simplifies the design
  2. Completely revert the way we develop.
  3. Makes us think about how our object would be used.
  4. Helps us develop better interfaces that are easier to use.
  5. Would change the way we perceive things.
  6. Makes the code easily testable
  7. Serves as invaluable form of documentation
  8. Makes the code robust
  9. Creates the opportunity for us to think of the failures and what we need to accommodate.
  10. Provides a safety net as we refactor the code – the test cases are our angels.

There are at least three things we need to write the test for:

  1. Positive: what is should do correctly assuming every thing is ideal.
  2. Negative: what could go wrong and how should the code behave.
  3. Exception: possibility of alternate sequence of events that could happen and how the code should behave to accommodate those.

Where to write a test:

  • Should be part of the project.
  • It can be within the class for testing private members.
Key things to keep in Mind:
  1. Red/Green Refactor should be our mantra
  2. Do not make many changes at once.
  3. Place the tests near the code
  4. Isolate your tests – failure of one should not affect the other.
  5. Write a test for a bug you find.
  6. Do not refactor code without having test cases to support.
  7. Test on all your platforms.

2 comments:

Unknown said...

Hey Ankur..came across your blog while browsing for pointers on TW...nice to know u made it through TW..actually i have applied thr n m done wid the code submission ..i have a telephonic scheduled sumtime nex week..no clue as to wat is expected in dat.. was jus looking for some help/tips on that front..wud be really kind if u cud spare some time to give a few pointers..my email is garima.n.gupta@gmail.com..looking fwd to a reply..-garima

Ankur Bhatia said...

Hey, thanks for dropping by...Congrats on clearing the code review test...many fall short in that...there is not much to prepare...go through your projects, basics OOPS concepts...thats about it...
Best of luck.