Thursday, 22 January 2009

All About TDD


Best Practices:
  • Test Behavior, Not Methods
  • Apply the "Too Simple to Break" Rule
  • Use Object-Oriented Best Practices In Your Test Cases
  • Use Appropriate Method Names
  • Don’t Use Try/Catch Blocks to Catch Unexpected Exceptions
  • Don’t Rely on Visual Inspection
  • Use A Code Coverage Tool
  • Use The Appropriate Tool for the Testing Scenario
  • Write the test first, then the code.
  • Design classes using dependency injection.
  • Separate UI code from its behavior using Model-View-Controller or Model-View-Presenter.
  • Do not write static methods or classes. Reason: Static methods are difficult or impossible to isolate and Rhino Mocks is unable to mock them.
  • Program off interfaces, not classes. Reason: Interfaces can be easily mocked using Rhino Mocks and other mocking frameworks.
  • Isolate external dependencies. Reason: Unresolved external dependencies cannot be tested.
  • Mark as virtual the methods you intend to mock. Reason: Rhino Mocks is unable to mock non-virtual members.
Tools:
Process of TDD:
  1. Receive requirement/feature as a User Story
  2. Write scenarios and behaviours of the feature in the Given/When/Then format. You'd need to be good at identifying behaviours/scenarios of a certain feature.
  3. Create your first test fixture and write a test method which tests a behaviour. You write the test to a non-existent Type. You'd need to be good in identifying which classes would need to be created; of course you can refactor later if you have time.
  4. Use Resharper to generate the non-existent class.
  5. Write enough code so that your code compiles
  6. Run the test and see it fails
  7. Write enough production code so that the test of the certain behaviour passes
  8. Refactor the production code and test code both.
  9. Go to 2 and cover more scenarios.
More Info:

No comments: